Skip to content

Commit 2a80089

Browse files
committed
Add acceptance tests for Job template data source
1 parent 4c967d5 commit 2a80089

File tree

4 files changed

+182
-2
lines changed

4 files changed

+182
-2
lines changed

.lranjbar

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export AAP_USERNAME=admin
2+
export AAP_PASSWORD=u2NYdSiPUXR8yxKYaqKIMl6NkYXnMEaV
3+
export AAP_TEST_JOB_TEMPLATE_ID=7

internal/provider/inventory_data_source_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestAccInventoryDataSource(t *testing.T) {
106106
PreCheck: func() { testAccPreCheck(t) },
107107
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
108108
Steps: []resource.TestStep{
109-
// Read
109+
// Create and Read
110110
{
111111
Config: testAccInventoryDataSource(randomName),
112112
Check: resource.ComposeAggregateTestCheckFunc(
@@ -117,6 +117,17 @@ func TestAccInventoryDataSource(t *testing.T) {
117117
resource.TestCheckResourceAttrPair("aap_inventory.test", "url", "data.aap_inventory.test", "url"),
118118
),
119119
},
120+
// Create and Read
121+
{
122+
Config: testAccInventoryDataSourceNamedUrl(randomName, "Default"),
123+
Check: resource.ComposeAggregateTestCheckFunc(
124+
resource.TestCheckResourceAttrPair("aap_inventory.test", "name", "data.aap_inventory.test", "name"),
125+
resource.TestCheckResourceAttrPair("aap_inventory.test", "organization", "data.aap_inventory.test", "organization"),
126+
resource.TestCheckResourceAttrPair("aap_inventory.test", "description", "data.aap_inventory.test", "description"),
127+
resource.TestCheckResourceAttrPair("aap_inventory.test", "variables", "data.aap_inventory.test", "variables"),
128+
resource.TestCheckResourceAttrPair("aap_inventory.test", "url", "data.aap_inventory.test", "url"),
129+
),
130+
},
120131
},
121132
CheckDestroy: testAccCheckInventoryResourceDestroy,
122133
})
@@ -137,3 +148,19 @@ data "aap_inventory" "test" {
137148
}
138149
`, name)
139150
}
151+
152+
func testAccInventoryDataSourceNamedUrl(name string, orgName string) string {
153+
return fmt.Sprintf(`
154+
resource "aap_inventory" "test" {
155+
name = "%s"
156+
organization = 1
157+
description = "A test inventory"
158+
variables = "{\"abc\": \"def\"}"
159+
}
160+
161+
data "aap_inventory" "test" {
162+
name = "%s"
163+
organization_name = "%s"
164+
}
165+
`, name, name, orgName)
166+
}

internal/provider/job_template_data_source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (d *JobTemplateDataSource) Read(ctx context.Context, req datasource.ReadReq
117117
return
118118
}
119119

120-
uri := path.Join(d.client.getApiEndpoint(), "inventories")
120+
uri := path.Join(d.client.getApiEndpoint(), "job_templates")
121121
resourceURL, err := ReturnAAPNamedURL(state.Id, state.Name, state.OrganizationName, uri)
122122
if err != nil {
123123
resp.Diagnostics.AddError("Minimal Data Not Supplied", "Require [id] or [name and organization_name]")
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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

Comments
 (0)