package google import ( "reflect" "testing" "fmt" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" "google.golang.org/api/servicemanagement/v1" ) func TestAccEndpointsService_basic(t *testing.T) { t.Parallel() random_name := "t-" + acctest.RandString(10) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { Config: testAccEndpointsService_basic(random_name), Check: testAccCheckEndpointExistsByName(random_name), }, }, }) } func TestAccEndpointsService_grpc(t *testing.T) { t.Parallel() random_name := "t-" + acctest.RandString(10) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { Config: testAccEndpointsService_grpc(random_name), Check: testAccCheckEndpointExistsByName(random_name), }, }, }) } func TestEndpointsService_grpcMigrateState(t *testing.T) { cases := map[string]struct { StateVersion int Attributes map[string]string ExpectedAttributes map[string]string Meta interface{} }{ "update from protoc_output to protoc_output_base64": { StateVersion: 0, Attributes: map[string]string{ "protoc_output": "123456789", "name": "testcase", }, ExpectedAttributes: map[string]string{ "protoc_output_base64": "MTIzNDU2Nzg5", "protoc_output": "", "name": "testcase", }, Meta: &Config{Project: "gcp-project", Region: "us-central1"}, }, "update from non-protoc_output": { StateVersion: 0, Attributes: map[string]string{ "openapi_config": "foo bar baz", "name": "testcase-2", }, ExpectedAttributes: map[string]string{ "openapi_config": "foo bar baz", "name": "testcase-2", }, Meta: &Config{Project: "gcp-project", Region: "us-central1"}, }, } for tn, tc := range cases { is := &terraform.InstanceState{ ID: tc.Attributes["name"], Attributes: tc.Attributes, } is, err := migrateEndpointsService(tc.StateVersion, is, tc.Meta) if err != nil { t.Fatalf("bad: %s, err: %#v", tn, err) } if !reflect.DeepEqual(is.Attributes, tc.ExpectedAttributes) { t.Fatalf("Attributes should be `%s` but are `%s`", tc.ExpectedAttributes, is.Attributes) } } } func testAccEndpointsService_basic(random_name string) string { return fmt.Sprintf(`resource "google_endpoints_service" "endpoints_service" { service_name = "%s.endpoints.%s.cloud.goog" project = "%s" openapi_config = <