From 39779e70210a2e4fedfb5a6ef51ce1a084fd45f1 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 14 Oct 2021 09:33:54 -0700 Subject: [PATCH] backend/remote-state/cos: Don't use github.com/likexian/gokit We don't use this library anywhere else in Terraform, and this backend was using it only for trivial helpers that are easy to express inline anyway. The new direct code is also type-checkable, whereas these helper functions seem to be written using reflection. This gives us one fewer dependency to worry about and makes the test code for this backend follow a similar assertions style as the rest of this codebase. --- go.mod | 1 - go.sum | 8 --- .../backend/remote-state/cos/backend_state.go | 11 ++- .../backend/remote-state/cos/backend_test.go | 67 +++++++++++++------ 4 files changed, 57 insertions(+), 30 deletions(-) diff --git a/go.mod b/go.mod index eab2aac97c..60b751726e 100644 --- a/go.mod +++ b/go.mod @@ -51,7 +51,6 @@ require ( github.com/joyent/triton-go v0.0.0-20180313100802-d8f9c0314926 github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 github.com/lib/pq v1.10.3 - github.com/likexian/gokit v0.20.15 github.com/lusis/go-artifactory v0.0.0-20160115162124-7e4ce345df82 github.com/masterzen/winrm v0.0.0-20200615185753-c42b5136ff88 github.com/mattn/go-isatty v0.0.12 diff --git a/go.sum b/go.sum index e14e6617a6..7d2eaf9415 100644 --- a/go.sum +++ b/go.sum @@ -460,14 +460,6 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0 github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lib/pq v1.10.3 h1:v9QZf2Sn6AmjXtQeFpdoq/eaNtYP6IN+7lcrygsIAtg= github.com/lib/pq v1.10.3/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= -github.com/likexian/gokit v0.0.0-20190309162924-0a377eecf7aa/go.mod h1:QdfYv6y6qPA9pbBA2qXtoT8BMKha6UyNbxWGWl/9Jfk= -github.com/likexian/gokit v0.0.0-20190418170008-ace88ad0983b/go.mod h1:KKqSnk/VVSW8kEyO2vVCXoanzEutKdlBAPohmGXkxCk= -github.com/likexian/gokit v0.0.0-20190501133040-e77ea8b19cdc/go.mod h1:3kvONayqCaj+UgrRZGpgfXzHdMYCAO0KAt4/8n0L57Y= -github.com/likexian/gokit v0.20.15 h1:DgtIqqTRFqtbiLJFzuRESwVrxWxfs8OlY6hnPYBa3BM= -github.com/likexian/gokit v0.20.15/go.mod h1:kn+nTv3tqh6yhor9BC4Lfiu58SmH8NmQ2PmEl+uM6nU= -github.com/likexian/simplejson-go v0.0.0-20190409170913-40473a74d76d/go.mod h1:Typ1BfnATYtZ/+/shXfFYLrovhFyuKvzwrdOnIDHlmg= -github.com/likexian/simplejson-go v0.0.0-20190419151922-c1f9f0b4f084/go.mod h1:U4O1vIJvIKwbMZKUJ62lppfdvkCdVd2nfMimHK81eec= -github.com/likexian/simplejson-go v0.0.0-20190502021454-d8787b4bfa0b/go.mod h1:3BWwtmKP9cXWwYCr5bkoVDEfLywacOv0s06OBEDpyt8= github.com/lusis/go-artifactory v0.0.0-20160115162124-7e4ce345df82 h1:wnfcqULT+N2seWf6y4yHzmi7GD2kNx4Ute0qArktD48= github.com/lusis/go-artifactory v0.0.0-20160115162124-7e4ce345df82/go.mod h1:y54tfGmO3NKssKveTEFFzH8C/akrSOy/iW9qEAUDV84= github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= diff --git a/internal/backend/remote-state/cos/backend_state.go b/internal/backend/remote-state/cos/backend_state.go index c9df2a6c18..ab92cfb7c0 100644 --- a/internal/backend/remote-state/cos/backend_state.go +++ b/internal/backend/remote-state/cos/backend_state.go @@ -11,7 +11,6 @@ import ( "github.com/hashicorp/terraform/internal/states" "github.com/hashicorp/terraform/internal/states/remote" "github.com/hashicorp/terraform/internal/states/statemgr" - "github.com/likexian/gokit/assert" ) // Define file suffix @@ -88,7 +87,15 @@ func (b *Backend) StateMgr(name string) (statemgr.Full, error) { return nil, err } - if !assert.IsContains(ws, name) { + exists := false + for _, candidate := range ws { + if candidate == name { + exists = true + break + } + } + + if !exists { log.Printf("[DEBUG] workspace %v not exists", name) // take a lock on this state while we write it diff --git a/internal/backend/remote-state/cos/backend_test.go b/internal/backend/remote-state/cos/backend_test.go index f527d4a5a5..eb9038ff35 100644 --- a/internal/backend/remote-state/cos/backend_test.go +++ b/internal/backend/remote-state/cos/backend_test.go @@ -9,7 +9,6 @@ import ( "github.com/hashicorp/terraform/internal/backend" "github.com/hashicorp/terraform/internal/states/remote" - "github.com/likexian/gokit/assert" ) const ( @@ -38,12 +37,18 @@ func TestStateFile(t *testing.T) { } for _, c := range cases { - b := &Backend{ - prefix: c.prefix, - key: c.key, - } - assert.Equal(t, b.stateFile(c.stateName), c.wantStateFile) - assert.Equal(t, b.lockFile(c.stateName), c.wantLockFile) + t.Run(fmt.Sprintf("%s %s %s", c.prefix, c.key, c.stateName), func(t *testing.T) { + b := &Backend{ + prefix: c.prefix, + key: c.key, + } + if got, want := b.stateFile(c.stateName), c.wantStateFile; got != want { + t.Errorf("wrong state file name\ngot: %s\nwant: %s", got, want) + } + if got, want := b.lockFile(c.stateName), c.wantLockFile; got != want { + t.Errorf("wrong lock file name\ngot: %s\nwant: %s", got, want) + } + }) } } @@ -56,10 +61,14 @@ func TestRemoteClient(t *testing.T) { defer teardownBackend(t, be) ss, err := be.StateMgr(backend.DefaultStateName) - assert.Nil(t, err) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } rs, ok := ss.(*remote.State) - assert.True(t, ok) + if !ok { + t.Fatalf("wrong state manager type\ngot: %T\nwant: %T", ss, rs) + } remote.TestClient(t, rs.Client) } @@ -74,10 +83,14 @@ func TestRemoteClientWithPrefix(t *testing.T) { defer teardownBackend(t, be) ss, err := be.StateMgr(backend.DefaultStateName) - assert.Nil(t, err) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } rs, ok := ss.(*remote.State) - assert.True(t, ok) + if !ok { + t.Fatalf("wrong state manager type\ngot: %T\nwant: %T", ss, rs) + } remote.TestClient(t, rs.Client) } @@ -91,10 +104,14 @@ func TestRemoteClientWithEncryption(t *testing.T) { defer teardownBackend(t, be) ss, err := be.StateMgr(backend.DefaultStateName) - assert.Nil(t, err) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } rs, ok := ss.(*remote.State) - assert.True(t, ok) + if !ok { + t.Fatalf("wrong state manager type\ngot: %T\nwant: %T", ss, rs) + } remote.TestClient(t, rs.Client) } @@ -122,10 +139,14 @@ func TestRemoteLocks(t *testing.T) { } c0, err := remoteClient() - assert.Nil(t, err) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } c1, err := remoteClient() - assert.Nil(t, err) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } remote.TestRemoteLocks(t, c0, c1) } @@ -203,10 +224,14 @@ func setupBackend(t *testing.T, bucket, prefix, key string, encrypt bool) backen be := b.(*Backend) c, err := be.client("tencentcloud") - assert.Nil(t, err) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } err = c.putBucket() - assert.Nil(t, err) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } return b } @@ -215,10 +240,14 @@ func teardownBackend(t *testing.T, b backend.Backend) { t.Helper() c, err := b.(*Backend).client("tencentcloud") - assert.Nil(t, err) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } err = c.deleteBucket(true) - assert.Nil(t, err) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } } func bucketName(t *testing.T) string {