Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JivusAyrus committed Nov 14, 2024
1 parent 9395d6f commit 7eb041e
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 72 deletions.
33 changes: 28 additions & 5 deletions internal/service/contract/data_source_cosmo_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type contractDataSourceModel struct {
AdmissionWebhookUrl types.String `tfsdk:"admission_webhook_url"`
AdmissionWebhookSecret types.String `tfsdk:"admission_webhook_secret"`
LabelMatchers types.Map `tfsdk:"label_matchers"`
ExcludeTags types.List `tfsdk:"exclude_tags"`
IncludeTags types.List `tfsdk:"include_tags"`
}

func (d *contractDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down Expand Up @@ -72,6 +74,14 @@ func (d *contractDataSource) Schema(ctx context.Context, req datasource.SchemaRe
MarkdownDescription: "The URL for the federated graph.",
Computed: true,
},
"exclude_tags": schema.ListAttribute{
Computed: true,
ElementType: types.StringType,
},
"include_tags": schema.ListAttribute{
Computed: true,
ElementType: types.StringType,
},
},
}
}
Expand Down Expand Up @@ -124,22 +134,35 @@ func (d *contractDataSource) Read(ctx context.Context, req datasource.ReadReques
}

graph := apiResponse.Graph

data.Id = types.StringValue(graph.GetId())
data.Name = types.StringValue(graph.GetName())
data.Namespace = types.StringValue(graph.GetNamespace())
data.RoutingURL = types.StringValue(graph.GetRoutingURL())

labelMatchers := make(map[string]attr.Value)
for _, labelMatcher := range graph.GetLabelMatchers() {
labelMatchers[labelMatcher] = types.StringValue(labelMatcher)
if graph.Contract != nil && len(graph.Contract.GetExcludeTags()) > 0 {
var responseExcludeTags []attr.Value
for _, tag := range graph.Contract.GetExcludeTags() {
responseExcludeTags = append(responseExcludeTags, types.StringValue(tag))
}
data.ExcludeTags = types.ListValueMust(types.StringType, responseExcludeTags)
}

if graph.Contract != nil && len(graph.Contract.GetIncludeTags()) > 0 {
var responseIncludeTags []attr.Value
for _, tag := range graph.Contract.IncludeTags {
responseIncludeTags = append(responseIncludeTags, types.StringValue(tag))
}
data.IncludeTags = types.ListValueMust(types.StringType, responseIncludeTags)
}
data.LabelMatchers = types.MapValueMust(types.StringType, labelMatchers)

if graph.Readme != nil {
data.Readme = types.StringValue(*graph.Readme)
}

if graph.GetAdmissionWebhookUrl() != "" {
data.AdmissionWebhookUrl = types.StringValue(*graph.AdmissionWebhookUrl)
}

tflog.Trace(ctx, "Read contract data source", map[string]interface{}{
"id": data.Id.ValueString(),
})
Expand Down
30 changes: 24 additions & 6 deletions internal/service/contract/data_source_cosmo_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ func TestAccContractDataSource(t *testing.T) {
name := acctest.RandomWithPrefix("test-contract")
namespace := acctest.RandomWithPrefix("test-namespace")

subgraphName := acctest.RandomWithPrefix("test-subgraph")
subgraphRoutingURL := "https://subgraph-standalone-example.com"
subgraphSchema := acceptance.TestAccValidSubgraphSchema

resource.Test(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acceptance.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccContractDataSourceConfig(namespace, name),
Config: testAccContractDataSourceConfig(namespace, subgraphName, subgraphRoutingURL, subgraphSchema, name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.cosmo_contract.test", "name", name),
resource.TestCheckResourceAttr("data.cosmo_contract.test", "namespace", namespace),
Expand All @@ -28,27 +32,41 @@ func TestAccContractDataSource(t *testing.T) {
ResourceName: "data.cosmo_contract.test",
RefreshState: true,
},
{
Config: testAccContractDataSourceConfig(namespace, subgraphName, subgraphRoutingURL, subgraphSchema, name),
Destroy: true,
},
},
})
}

func testAccContractDataSourceConfig(namespace, name string) string {
func testAccContractDataSourceConfig(namespace, subgraphName, subgraphRoutingURL, subgraphSchema, name string) string {
return fmt.Sprintf(`
resource "cosmo_namespace" "test" {
name = "%s"
}
resource "cosmo_monograph" "source_graph" {
resource "cosmo_federated_graph" "source_graph" {
name = "source-graph"
namespace = cosmo_namespace.test.name
routing_url = "https://example.com"
graph_url = "https://example.com"
depends_on = [cosmo_subgraph.test]
}
resource "cosmo_subgraph" "test" {
name = "%s"
namespace = cosmo_namespace.test.name
routing_url = "%s"
schema = <<-EOT
%s
EOT
labels = {}
}
resource "cosmo_contract" "test" {
name = "%s"
namespace = cosmo_namespace.test.name
source = cosmo_monograph.source_graph.name
source = cosmo_federated_graph.source_graph.name
routing_url = "https://example.com"
readme = "Initial readme content"
}
Expand All @@ -57,5 +75,5 @@ data "cosmo_contract" "test" {
name = cosmo_contract.test.name
namespace = cosmo_contract.test.namespace
}
`, namespace, name)
`, namespace, subgraphName, subgraphRoutingURL, subgraphSchema, name)
}
83 changes: 51 additions & 32 deletions internal/service/contract/resource_cosmo_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,27 @@ func (r *contractResource) Create(ctx context.Context, req resource.CreateReques
data.Namespace = types.StringValue(graph.GetNamespace())
data.RoutingURL = types.StringValue(graph.GetRoutingURL())

var responseExcludeTags []attr.Value
for _, tag := range graph.Contract.GetExcludeTags() {
responseExcludeTags = append(responseExcludeTags, types.StringValue(tag))
if graph.Contract != nil && len(graph.Contract.GetExcludeTags()) > 0 {
var responseExcludeTags []attr.Value
for _, tag := range graph.Contract.GetExcludeTags() {
responseExcludeTags = append(responseExcludeTags, types.StringValue(tag))
}
data.ExcludeTags = types.ListValueMust(types.StringType, responseExcludeTags)
}
data.ExcludeTags = types.ListValueMust(types.StringType, responseExcludeTags)

var responseIncludeTags []attr.Value
for _, tag := range graph.Contract.IncludeTags {
responseIncludeTags = append(responseIncludeTags, types.StringValue(tag))
if graph.Contract != nil && len(graph.Contract.GetIncludeTags()) > 0 {
var responseIncludeTags []attr.Value
for _, tag := range graph.Contract.IncludeTags {
responseIncludeTags = append(responseIncludeTags, types.StringValue(tag))
}
data.IncludeTags = types.ListValueMust(types.StringType, responseIncludeTags)
}
data.IncludeTags = types.ListValueMust(types.StringType, responseIncludeTags)

if graph.Readme != nil {
data.Readme = types.StringValue(*graph.Readme)
}

if graph.AdmissionWebhookUrl != nil {
if graph.GetAdmissionWebhookUrl() != "" {
data.AdmissionWebhookUrl = types.StringValue(*graph.AdmissionWebhookUrl)
}

Expand Down Expand Up @@ -192,23 +196,27 @@ func (r *contractResource) Read(ctx context.Context, req resource.ReadRequest, r
data.Namespace = types.StringValue(graph.GetNamespace())
data.RoutingURL = types.StringValue(graph.GetRoutingURL())

var responseExcludeTags []attr.Value
for _, tag := range graph.Contract.GetExcludeTags() {
responseExcludeTags = append(responseExcludeTags, types.StringValue(tag))
if graph.Contract != nil && len(graph.Contract.GetExcludeTags()) > 0 {
var responseExcludeTags []attr.Value
for _, tag := range graph.Contract.GetExcludeTags() {
responseExcludeTags = append(responseExcludeTags, types.StringValue(tag))
}
data.ExcludeTags = types.ListValueMust(types.StringType, responseExcludeTags)
}
data.ExcludeTags = types.ListValueMust(types.StringType, responseExcludeTags)

var responseIncludeTags []attr.Value
for _, tag := range graph.Contract.IncludeTags {
responseIncludeTags = append(responseIncludeTags, types.StringValue(tag))
if graph.Contract != nil && len(graph.Contract.GetIncludeTags()) > 0 {
var responseIncludeTags []attr.Value
for _, tag := range graph.Contract.IncludeTags {
responseIncludeTags = append(responseIncludeTags, types.StringValue(tag))
}
data.IncludeTags = types.ListValueMust(types.StringType, responseIncludeTags)
}
data.IncludeTags = types.ListValueMust(types.StringType, responseIncludeTags)

if graph.Readme != nil {
data.Readme = types.StringValue(*graph.Readme)
}

if graph.AdmissionWebhookUrl != nil {
if graph.GetAdmissionWebhookUrl() != "" {
data.AdmissionWebhookUrl = types.StringValue(*graph.AdmissionWebhookUrl)
}

Expand Down Expand Up @@ -250,11 +258,18 @@ func (r *contractResource) Update(ctx context.Context, req resource.UpdateReques

_, apiError := r.client.UpdateContract(ctx, data.Name.ValueString(), data.Namespace.ValueString(), excludeTags, includeTags)
if apiError != nil {
utils.AddDiagnosticError(resp,
ErrUpdatingContract,
apiError.Error(),
)
return
if api.IsContractCompositionFailedError(apiError) || api.IsSubgraphCompositionFailedError(apiError) {
utils.AddDiagnosticError(resp,
ErrUpdatingContract,
apiError.Error(),
)
} else {
utils.AddDiagnosticError(resp,
ErrUpdatingContract,
apiError.Error(),
)
return
}
}

response, apiError := r.client.GetFederatedGraph(ctx, data.Name.ValueString(), data.Namespace.ValueString())
Expand All @@ -272,23 +287,27 @@ func (r *contractResource) Update(ctx context.Context, req resource.UpdateReques
data.Namespace = types.StringValue(graph.GetNamespace())
data.RoutingURL = types.StringValue(graph.GetRoutingURL())

var responseExcludeTags []attr.Value
for _, tag := range graph.Contract.GetExcludeTags() {
responseExcludeTags = append(responseExcludeTags, types.StringValue(tag))
if graph.Contract != nil && len(graph.Contract.GetExcludeTags()) > 0 {
var responseExcludeTags []attr.Value
for _, tag := range graph.Contract.GetExcludeTags() {
responseExcludeTags = append(responseExcludeTags, types.StringValue(tag))
}
data.ExcludeTags = types.ListValueMust(types.StringType, responseExcludeTags)
}
data.ExcludeTags = types.ListValueMust(types.StringType, responseExcludeTags)

var responseIncludeTags []attr.Value
for _, tag := range graph.Contract.IncludeTags {
responseIncludeTags = append(responseIncludeTags, types.StringValue(tag))
if graph.Contract != nil && len(graph.Contract.GetIncludeTags()) > 0 {
var responseIncludeTags []attr.Value
for _, tag := range graph.Contract.IncludeTags {
responseIncludeTags = append(responseIncludeTags, types.StringValue(tag))
}
data.IncludeTags = types.ListValueMust(types.StringType, responseIncludeTags)
}
data.IncludeTags = types.ListValueMust(types.StringType, responseIncludeTags)

if graph.Readme != nil {
data.Readme = types.StringValue(*graph.Readme)
}

if graph.AdmissionWebhookUrl != nil {
if graph.GetAdmissionWebhookUrl() != "" {
data.AdmissionWebhookUrl = types.StringValue(*graph.AdmissionWebhookUrl)
}

Expand Down
36 changes: 28 additions & 8 deletions internal/service/contract/resource_cosmo_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,65 @@ func TestAccContractResource(t *testing.T) {
readme := "Initial readme content"

federatedGraphName := acctest.RandomWithPrefix("test-federated-graph")
federatedGraphroutingURL := "https://example.com:3000"
federatedGraphRoutingURL := "https://example.com:3000"

graphUrl := "http://example.com/graphql"
subgraphName := acctest.RandomWithPrefix("test-subgraph")
subgraphRoutingURL := "https://subgraph-standalone-example.com"
subgraphSchema := acceptance.TestAccValidSubgraphSchema

resource.Test(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acceptance.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccContractResourceConfig(namespace, federatedGraphName, federatedGraphroutingURL, graphUrl, name, readme),
Config: testAccContractResourceConfig(namespace, federatedGraphName, federatedGraphRoutingURL, subgraphName, subgraphRoutingURL, subgraphSchema, name, readme),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("cosmo_contract.test", "name", name),
resource.TestCheckResourceAttr("cosmo_contract.test", "namespace", namespace),
resource.TestCheckResourceAttr("cosmo_contract.test", "readme", readme),
),
},
{
ResourceName: "cosmo_contract.test",
RefreshState: true,
},
{
Config: testAccContractResourceConfig(namespace, federatedGraphName, federatedGraphRoutingURL, subgraphName, subgraphRoutingURL, subgraphSchema, name, readme),
Destroy: true,
},
},
})
}

func testAccContractResourceConfig(namespace, federatedGraphName, federatedGraphroutingURL, graphUrl, contractName, contractReadme string) string {
func testAccContractResourceConfig(namespace, federatedGraphName, federatedGraphRoutingURL, subgraphName, subgraphRoutingURL, subgraphSchema, contractName, contractReadme string) string {
return fmt.Sprintf(`
resource "cosmo_namespace" "test" {
name = "%s"
}
resource "cosmo_monograph" "test" {
resource "cosmo_federated_graph" "test" {
name = "%s"
namespace = cosmo_namespace.test.name
routing_url = "%s"
graph_url = "%s"
depends_on = [cosmo_subgraph.test]
}
resource "cosmo_subgraph" "test" {
name = "%s"
namespace = cosmo_namespace.test.name
routing_url = "%s"
schema = <<-EOT
%s
EOT
labels = {}
}
resource "cosmo_contract" "test" {
name = "%s"
namespace = cosmo_namespace.test.name
source = cosmo_monograph.test.name
source = cosmo_federated_graph.test.name
routing_url = "http://localhost:3003"
readme = "%s"
}
`, namespace, federatedGraphName, federatedGraphroutingURL, graphUrl, contractName, contractReadme)
`, namespace, federatedGraphName, federatedGraphRoutingURL, subgraphName, subgraphRoutingURL, subgraphSchema, contractName, contractReadme)
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func (d *FederatedGraphDataSource) Read(ctx context.Context, req datasource.Read
data.Readme = types.StringValue(*graph.Readme)
}

if graph.GetAdmissionWebhookUrl() != "" {
data.AdmissionWebhookUrl = types.StringValue(*graph.AdmissionWebhookUrl)
}

tflog.Trace(ctx, "Read federated graph data source", map[string]interface{}{
"id": data.Id.ValueString(),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func TestAccFederatedGraphDataSource(t *testing.T) {
ResourceName: "data.cosmo_federated_graph.test",
RefreshState: true,
},
{
Config: testAccFederatedGraphDataSourceConfig(namespace, name),
Destroy: true,
},
},
})
}
Expand Down
Loading

0 comments on commit 7eb041e

Please sign in to comment.