Skip to content

Commit

Permalink
Merge pull request #4 from GK-Consulting/sk/add_tests
Browse files Browse the repository at this point in the history
Add Workspace Resource Tests
  • Loading branch information
OpenGLShaders authored Dec 24, 2023
2 parents 23bebc4 + c08fda2 commit 64a500d
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
37 changes: 26 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,25 @@ require (
github.com/openglshaders/astronomer-api/v2 v2.0.0-00010101000000-000000000000
)

require (
github.com/agext/levenshtein v1.2.2 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
github.com/hashicorp/hcl/v2 v2.19.1 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
google.golang.org/appengine v1.6.7 // indirect
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
Expand All @@ -31,15 +45,16 @@ require (
github.com/hashicorp/go-plugin v1.5.2 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hc-install v0.5.2 // indirect
github.com/hashicorp/terraform-exec v0.18.1 // indirect
github.com/hashicorp/terraform-json v0.17.1 // indirect
github.com/hashicorp/hc-install v0.6.1 // indirect
github.com/hashicorp/terraform-exec v0.19.0 // indirect
github.com/hashicorp/terraform-json v0.18.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.19.1 // indirect
github.com/hashicorp/terraform-plugin-testing v1.6.0
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mitchellh/cli v1.1.5 // indirect
Expand All @@ -53,13 +68,13 @@ require (
github.com/spf13/cast v1.5.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/zclconf/go-cty v1.13.2 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
golang.org/x/mod v0.11.0 // indirect
github.com/zclconf/go-cty v1.14.1 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
Expand Down
88 changes: 88 additions & 0 deletions go.sum

Large diffs are not rendered by default.

30 changes: 26 additions & 4 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic=
github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU=
github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow=
github.com/go-git/go-git/v5 v5.9.0 h1:cD9SFA7sHVRdJ7AYck1ZaAa/yeuBvGPxwXDL8cxrObY=
github.com/go-git/go-git/v5 v5.9.0/go.mod h1:RKIqga24sWdMGZF+1Ekv9kylsDz6LzdTSI2s/OsZWE0=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
github.com/skeema/knownhosts v1.2.0 h1:h9r9cf0+u7wSE+M183ZtMGgOJKiL96brpaz5ekfJCpM=
github.com/skeema/knownhosts v1.2.0/go.mod h1:g4fPeYpque7P0xefxtGzV81ihjC8sX2IqpAoNkjxbMo=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d h1:VBu5YqKPv6XiJ199exd8Br+Aetz+o08F+PLMnwJQHAY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
15 changes: 10 additions & 5 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

var _ provider.Provider = &AstronomerProvider{}
Expand Down Expand Up @@ -61,16 +62,20 @@ func (p *AstronomerProvider) Configure(ctx context.Context, req provider.Configu

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

if data.Token.IsNull() {
data.Token = types.StringValue(os.Getenv("ASTRONOMER_API_TOKEN"))
}

if data.Token.ValueString() == "" {
panic("No api key provided - either via provider configuration or environment variable.")
tflog.Error(ctx, "No api key provided - either via provider configuration or ASTRONOMER_API_TOKEN environment variable.")
resp.Diagnostics.AddError(
"API Key Not Found",
"No api key provided - either via provider configuration or ASTRONOMER_API_TOKEN environment variable.",
)
}

if resp.Diagnostics.HasError() {
return
}

dataSourceModel := new(AstronomerProviderDataSourceDataModel)
Expand Down
22 changes: 22 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package provider

import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)

var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"astronomer": providerserver.NewProtocol6WithError(New("test")()),
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("ASTRONOMER_API_TOKEN"); v == "" {
t.Fatal("ASTRONOMER_API_TOKEN must be set for acceptance tests")
}
if v := os.Getenv("ORGANIZATION_ID"); v == "" {
t.Fatal("ORGANIZATION_ID must be set for acceptance tests")
}
}
53 changes: 53 additions & 0 deletions internal/provider/workspace_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package provider

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestWorkspaceDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testWorkspaceDataSourceConfig("Data Source Test Workspace"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.astronomer_workspace.test", "name", "Data Source Test Workspace"),
resource.TestCheckResourceAttr("data.astronomer_workspace.test", "cicd_enforced_default", "true"),
resource.TestCheckResourceAttr("data.astronomer_workspace.test", "description", "TestAccDataSource"),
),
},
},
})
}

func testWorkspaceDataSourceConfig(workspaceName string) string {
orgId := os.Getenv("ORGANIZATION_ID")
return fmt.Sprintf(`
terraform {
required_providers {
astronomer = {
source = "registry.terraform.io/gk-consulting/astronomer"
}
}
}
provider "astronomer" {
organization_id = %[1]q
}
resource "astronomer_workspace" "test" {
name = %[2]q
cicd_enforced_default = true
description = "TestAccDataSource"
}
data "astronomer_workspace" "test" {
id = astronomer_workspace.test.id
}
`, orgId, workspaceName)
}
2 changes: 0 additions & 2 deletions internal/provider/workspace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func (r *WorkspaceResource) Configure(ctx context.Context, req resource.Configur
func (r *WorkspaceResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data WorkspaceResourceModel

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -172,7 +171,6 @@ func (r *WorkspaceResource) Delete(ctx context.Context, req resource.DeleteReque
}
}

// TODO figure out what this does
func (r *WorkspaceResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
58 changes: 58 additions & 0 deletions internal/provider/workspace_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package provider

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccWorkspaceResource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccWorkspaceResourceConfig("one"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("astronomer_workspace.test", "name", "one"),
resource.TestCheckResourceAttr("astronomer_workspace.test", "cicd_enforced_default", "true"),
resource.TestCheckResourceAttr("astronomer_workspace.test", "description", "TestAcc"),
),
},
{
ResourceName: "astronomer_workspace.test",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccWorkspaceResourceConfig("two"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("astronomer_workspace.test", "name", "two"),
),
},
},
})
}

func testAccWorkspaceResourceConfig(name string) string {
orgId := os.Getenv("ORGANIZATION_ID")
return fmt.Sprintf(`
terraform {
required_providers {
astronomer = {
source = "registry.terraform.io/gk-consulting/astronomer"
}
}
}
provider "astronomer" {
organization_id = %[1]q
}
resource "astronomer_workspace" "test" {
name = %[2]q
cicd_enforced_default = true
description = "TestAcc"
}
`, orgId, name)
}

0 comments on commit 64a500d

Please sign in to comment.