|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/ansible/terraform-provider-aap/internal/provider/customtypes" |
| 10 | + fwdatasource "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 13 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 14 | +) |
| 15 | + |
| 16 | +func TestJobTemplateDataSourceSchema(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + ctx := context.Background() |
| 20 | + schemaRequest := fwdatasource.SchemaRequest{} |
| 21 | + schemaResponse := &fwdatasource.SchemaResponse{} |
| 22 | + |
| 23 | + // Instantiate the InventoryDataSource and call its Schema method |
| 24 | + NewJobTemplateDataSource().Schema(ctx, schemaRequest, schemaResponse) |
| 25 | + |
| 26 | + if schemaResponse.Diagnostics.HasError() { |
| 27 | + t.Fatalf("Schema method diagnostics: %+v", schemaResponse.Diagnostics) |
| 28 | + } |
| 29 | + |
| 30 | + // Validate the schema |
| 31 | + diagnostics := schemaResponse.Schema.ValidateImplementation(ctx) |
| 32 | + |
| 33 | + if diagnostics.HasError() { |
| 34 | + t.Fatalf("Schema validation diagnostics: %+v", diagnostics) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func TestJobTemplateDataSourceParseHttpResponse(t *testing.T) { |
| 39 | + jsonError := diag.Diagnostics{} |
| 40 | + jsonError.AddError("Error parsing JSON response from AAP", "invalid character 'N' looking for beginning of value") |
| 41 | + |
| 42 | + var testTable = []struct { |
| 43 | + name string |
| 44 | + input []byte |
| 45 | + expected JobTemplateDataSourceModel |
| 46 | + errors diag.Diagnostics |
| 47 | + }{ |
| 48 | + { |
| 49 | + name: "JSON error", |
| 50 | + input: []byte("Not valid JSON"), |
| 51 | + expected: JobTemplateDataSourceModel{}, |
| 52 | + errors: jsonError, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "missing values", |
| 56 | + input: []byte(`{"id":1,"organization":2,"url":"/job_templates/1/"}`), |
| 57 | + expected: JobTemplateDataSourceModel{ |
| 58 | + Id: types.Int64Value(1), |
| 59 | + Organization: types.Int64Value(2), |
| 60 | + OrganizationName: types.StringValue(""), |
| 61 | + Url: types.StringValue("/job_templates/1/"), |
| 62 | + NamedUrl: types.StringValue(""), |
| 63 | + Name: types.StringNull(), |
| 64 | + Description: types.StringNull(), |
| 65 | + Variables: customtypes.NewAAPCustomStringNull(), |
| 66 | + }, |
| 67 | + errors: diag.Diagnostics{}, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "all values", |
| 71 | + input: []byte( |
| 72 | + `{"id":1,"organization":2,"url":"/job_templates/1/","name":"my job template","description":"My Test Job Template","variables":"{\"foo\":\"bar\"}"}`, |
| 73 | + ), |
| 74 | + expected: JobTemplateDataSourceModel{ |
| 75 | + Id: types.Int64Value(1), |
| 76 | + Organization: types.Int64Value(2), |
| 77 | + OrganizationName: types.StringValue(""), |
| 78 | + Url: types.StringValue("/job_templates/1/"), |
| 79 | + NamedUrl: types.StringValue(""), |
| 80 | + Name: types.StringValue("my job template"), |
| 81 | + Description: types.StringValue("My Test Job Template"), |
| 82 | + Variables: customtypes.NewAAPCustomStringValue("{\"foo\":\"bar\"}"), |
| 83 | + }, |
| 84 | + errors: diag.Diagnostics{}, |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + for _, test := range testTable { |
| 89 | + t.Run(test.name, func(t *testing.T) { |
| 90 | + source := JobTemplateDataSourceModel{} |
| 91 | + diags := source.ParseHttpResponse(test.input) |
| 92 | + if !test.errors.Equal(diags) { |
| 93 | + t.Errorf("Expected error diagnostics (%s), Received (%s)", test.errors, diags) |
| 94 | + } |
| 95 | + if test.expected != source { |
| 96 | + t.Errorf("Expected (%s) not equal to actual (%s)", test.expected, source) |
| 97 | + } |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestAccJobTemplateDataSource(t *testing.T) { |
| 103 | + jobTemplateID := os.Getenv("AAP_TEST_JOB_TEMPLATE_ID") |
| 104 | + jobTemplateName := "Demo Job Template" |
| 105 | + jobTemplateOrg := "Default" |
| 106 | + |
| 107 | + resource.Test(t, resource.TestCase{ |
| 108 | + PreCheck: func() { testAccPreCheck(t) }, |
| 109 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 110 | + Steps: []resource.TestStep{ |
| 111 | + // Read |
| 112 | + { |
| 113 | + Config: testAccJobTemplateDataSourceFromId(jobTemplateID), |
| 114 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 115 | + resource.TestCheckResourceAttrSet("data.aap_job_template.test", "name"), |
| 116 | + resource.TestCheckResourceAttrSet("data.aap_job_template.test", "organization"), |
| 117 | + resource.TestCheckResourceAttrSet("data.aap_job_template.test", "url"), |
| 118 | + ), |
| 119 | + }, |
| 120 | + // Read |
| 121 | + { |
| 122 | + Config: testAccJobTemplateDataSourceFromNamedUrl(jobTemplateName, jobTemplateOrg), |
| 123 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 124 | + resource.TestCheckResourceAttrSet("data.aap_job_template.test", "name"), |
| 125 | + resource.TestCheckResourceAttrSet("data.aap_job_template.test", "organization"), |
| 126 | + resource.TestCheckResourceAttrSet("data.aap_job_template.test", "url"), |
| 127 | + ), |
| 128 | + }, |
| 129 | + }, |
| 130 | + CheckDestroy: testAccCheckInventoryResourceDestroy, |
| 131 | + }) |
| 132 | +} |
| 133 | + |
| 134 | +// testAccInventoryDataSource configures the Inventory Data Source for testing |
| 135 | +func testAccJobTemplateDataSourceFromId(id string) string { |
| 136 | + return fmt.Sprintf(` |
| 137 | +data "aap_job_template" "test" { |
| 138 | + id = %s |
| 139 | +} |
| 140 | +`, id) |
| 141 | +} |
| 142 | + |
| 143 | +func testAccJobTemplateDataSourceFromNamedUrl(name string, orgName string) string { |
| 144 | + return fmt.Sprintf(` |
| 145 | +data "aap_job_template" "test" { |
| 146 | + name = "%s" |
| 147 | + organization_name = "%s" |
| 148 | +} |
| 149 | +`, name, orgName) |
| 150 | +} |
0 commit comments