@@ -4,13 +4,15 @@ import (
44 "fmt"
55 "testing"
66
7+ "github.com/alexedwards/argon2id"
78 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
89 "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
910 containerSDK "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
1011 "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
1112 "github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1213 "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/container"
1314 containerchecks "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/container/testfuncs"
15+ "github.com/stretchr/testify/assert"
1416)
1517
1618func TestAccContainer_Basic (t * testing.T ) {
@@ -142,6 +144,7 @@ func TestAccContainer_Env(t *testing.T) {
142144 }
143145 secret_environment_variables = {
144146 "test_secret" = "test_secret"
147+ "first_secret" = "first_secret"
145148 }
146149 }
147150 ` ,
@@ -150,7 +153,8 @@ func TestAccContainer_Env(t *testing.T) {
150153 acctest .CheckResourceAttrUUID ("scaleway_container_namespace.main" , "id" ),
151154 acctest .CheckResourceAttrUUID ("scaleway_container.main" , "id" ),
152155 resource .TestCheckResourceAttr ("scaleway_container.main" , "environment_variables.test" , "test" ),
153- resource .TestCheckResourceAttr ("scaleway_container.main" , "secret_environment_variables.test_secret" , "test_secret" ),
156+ passwordMatchHash ("scaleway_container.main" , "secret_environment_variables.test_secret" , "test_secret" ),
157+ passwordMatchHash ("scaleway_container.main" , "secret_environment_variables.first_secret" , "first_secret" ),
154158 ),
155159 },
156160 {
@@ -165,6 +169,7 @@ func TestAccContainer_Env(t *testing.T) {
165169 }
166170 secret_environment_variables = {
167171 "foo_secret" = "bar_secret"
172+ "test_secret" = "updated_secret"
168173 }
169174 }
170175 ` ,
@@ -173,7 +178,9 @@ func TestAccContainer_Env(t *testing.T) {
173178 acctest .CheckResourceAttrUUID ("scaleway_container_namespace.main" , "id" ),
174179 acctest .CheckResourceAttrUUID ("scaleway_container.main" , "id" ),
175180 resource .TestCheckResourceAttr ("scaleway_container.main" , "environment_variables.foo" , "bar" ),
176- resource .TestCheckResourceAttr ("scaleway_container.main" , "secret_environment_variables.foo_secret" , "bar_secret" ),
181+ passwordMatchHash ("scaleway_container.main" , "secret_environment_variables.foo_secret" , "bar_secret" ),
182+ passwordMatchHash ("scaleway_container.main" , "secret_environment_variables.test_secret" , "updated_secret" ),
183+ resource .TestCheckNoResourceAttr ("scaleway_container.main" , "secret_environment_variables.first_secret" ),
177184 ),
178185 },
179186 {
@@ -601,3 +608,47 @@ func isContainerDestroyed(tt *acctest.TestTools) resource.TestCheckFunc {
601608 return nil
602609 }
603610}
611+
612+ func passwordMatchHash (parent string , key string , password string ) resource.TestCheckFunc {
613+ return func (state * terraform.State ) error {
614+ rs , ok := state .RootModule ().Resources [parent ]
615+ if ! ok {
616+ return fmt .Errorf ("resource container not found: %s" , parent )
617+ }
618+
619+ match , err := argon2id .ComparePasswordAndHash (password , rs .Primary .Attributes [key ])
620+ if err != nil {
621+ fmt .Println ("ARGON ERROR " , password , key , rs .Primary .Attributes [key ])
622+ return err
623+ }
624+
625+ if ! match {
626+ return fmt .Errorf ("password and hash do not match" )
627+ }
628+
629+ return nil
630+ }
631+ }
632+
633+ func TestFilterSecretEnvsToPatch (t * testing.T ) {
634+ testSecret := "test_secret"
635+ secretToDelete := "secret_to_delete"
636+ updatedSecret := "updated_secret"
637+ newSecret := "new_secret"
638+
639+ oldEnv := []* containerSDK.Secret {
640+ {Key : testSecret , Value : & testSecret },
641+ {Key : secretToDelete , Value : & secretToDelete },
642+ }
643+ newEnv := []* containerSDK.Secret {
644+ {Key : testSecret , Value : & updatedSecret },
645+ {Key : newSecret , Value : & newSecret },
646+ }
647+
648+ toPatch := container .FilterSecretEnvsToPatch (oldEnv , newEnv )
649+ assert .Equal (t , toPatch , []* containerSDK.Secret {
650+ {Key : testSecret , Value : & updatedSecret },
651+ {Key : newSecret , Value : & newSecret },
652+ {Key : secretToDelete , Value : nil },
653+ })
654+ }
0 commit comments