|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"os" |
| 6 | +"regexp" |
| 7 | +"strings" |
| 8 | +"testing" |
| 9 | +"text/template" |
| 10 | + |
| 11 | +"github.com/coder/coder/v2/codersdk" |
| 12 | +"github.com/coder/terraform-provider-coderd/integration" |
| 13 | +"github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 14 | +"github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +funcTestAccGroupDataSource(t*testing.T) { |
| 18 | +ifos.Getenv("TF_ACC")=="" { |
| 19 | +t.Skip("Acceptance tests are disabled.") |
| 20 | +} |
| 21 | +ctx:=context.Background() |
| 22 | +client:=integration.StartCoder(ctx,t,"group_acc") |
| 23 | +firstUser,err:=client.User(ctx,codersdk.Me) |
| 24 | +require.NoError(t,err) |
| 25 | + |
| 26 | +user1,err:=client.CreateUser(ctx, codersdk.CreateUserRequest{ |
| 27 | +Email:"example@coder.com", |
| 28 | +Username:"example", |
| 29 | +Password:"SomeSecurePassword!", |
| 30 | +UserLoginType:"password", |
| 31 | +OrganizationID:firstUser.OrganizationIDs[0], |
| 32 | +}) |
| 33 | +require.NoError(t,err) |
| 34 | + |
| 35 | +user2,err:=client.CreateUser(ctx, codersdk.CreateUserRequest{ |
| 36 | +Email:"example2@coder.com", |
| 37 | +Username:"example2", |
| 38 | +Password:"SomeSecurePassword!", |
| 39 | +UserLoginType:"password", |
| 40 | +OrganizationID:firstUser.OrganizationIDs[0], |
| 41 | +}) |
| 42 | +require.NoError(t,err) |
| 43 | + |
| 44 | +group,err:=client.CreateGroup(ctx,firstUser.OrganizationIDs[0], codersdk.CreateGroupRequest{ |
| 45 | +Name:"example-group", |
| 46 | +DisplayName:"Example Group", |
| 47 | +AvatarURL:"https://google.com", |
| 48 | +QuotaAllowance:10, |
| 49 | +}) |
| 50 | +require.NoError(t,err) |
| 51 | +group,err=client.PatchGroup(ctx,group.ID, codersdk.PatchGroupRequest{ |
| 52 | +AddUsers: []string{user1.ID.String(),user2.ID.String()}, |
| 53 | +}) |
| 54 | +require.NoError(t,err) |
| 55 | + |
| 56 | +checkFn:=resource.ComposeAggregateTestCheckFunc( |
| 57 | +resource.TestCheckResourceAttr("data.coderd_group.test","id",group.ID.String()), |
| 58 | +resource.TestCheckResourceAttr("data.coderd_group.test","name","example-group"), |
| 59 | +resource.TestCheckResourceAttr("data.coderd_group.test","organization_id",firstUser.OrganizationIDs[0].String()), |
| 60 | +resource.TestCheckResourceAttr("data.coderd_group.test","display_name","Example Group"), |
| 61 | +resource.TestCheckResourceAttr("data.coderd_group.test","avatar_url","https://google.com"), |
| 62 | +resource.TestCheckResourceAttr("data.coderd_group.test","quota_allowance","10"), |
| 63 | +resource.TestCheckResourceAttr("data.coderd_group.test","members.#","2"), |
| 64 | +resource.TestCheckTypeSetElemAttr("data.coderd_group.test","members.*",user1.ID.String()), |
| 65 | +resource.TestCheckTypeSetElemAttr("data.coderd_group.test","members.*",user2.ID.String()), |
| 66 | +resource.TestCheckResourceAttr("data.coderd_group.test","source","user"), |
| 67 | +) |
| 68 | + |
| 69 | +t.Run("GroupByIDOk",func(t*testing.T) { |
| 70 | +cfg:=testAccGroupDataSourceConfig{ |
| 71 | +URL:client.URL.String(), |
| 72 | +Token:client.SessionToken(), |
| 73 | +ID:PtrTo(group.ID.String()), |
| 74 | +} |
| 75 | +resource.Test(t, resource.TestCase{ |
| 76 | +PreCheck:func() {testAccPreCheck(t) }, |
| 77 | +ProtoV6ProviderFactories:testAccProtoV6ProviderFactories, |
| 78 | +Steps: []resource.TestStep{ |
| 79 | +{ |
| 80 | +Config:cfg.String(t), |
| 81 | +Check:checkFn, |
| 82 | +}, |
| 83 | +}, |
| 84 | +}) |
| 85 | +}) |
| 86 | + |
| 87 | +t.Run("GroupByNameAndOrganizationIDOk",func(t*testing.T) { |
| 88 | +cfg:=testAccGroupDataSourceConfig{ |
| 89 | +URL:client.URL.String(), |
| 90 | +Token:client.SessionToken(), |
| 91 | +OrganizationID:PtrTo(firstUser.OrganizationIDs[0].String()), |
| 92 | +Name:PtrTo("example-group"), |
| 93 | +} |
| 94 | +resource.Test(t, resource.TestCase{ |
| 95 | +PreCheck:func() {testAccPreCheck(t) }, |
| 96 | +ProtoV6ProviderFactories:testAccProtoV6ProviderFactories, |
| 97 | +Steps: []resource.TestStep{ |
| 98 | +{ |
| 99 | +Config:cfg.String(t), |
| 100 | +Check:checkFn, |
| 101 | +}, |
| 102 | +}, |
| 103 | +}) |
| 104 | +}) |
| 105 | + |
| 106 | +t.Run("GroupNameOnlyError",func(t*testing.T) { |
| 107 | +cfg:=testAccGroupDataSourceConfig{ |
| 108 | +URL:client.URL.String(), |
| 109 | +Token:client.SessionToken(), |
| 110 | +Name:PtrTo("example-group"), |
| 111 | +} |
| 112 | +resource.Test(t, resource.TestCase{ |
| 113 | +PreCheck:func() {testAccPreCheck(t) }, |
| 114 | +ProtoV6ProviderFactories:testAccProtoV6ProviderFactories, |
| 115 | +// Neither ID nor Username |
| 116 | +Steps: []resource.TestStep{ |
| 117 | +{ |
| 118 | +Config:cfg.String(t), |
| 119 | +ExpectError:regexp.MustCompile(`Attribute "organization_id" must be specified when "name" is specified`), |
| 120 | +}, |
| 121 | +}, |
| 122 | +}) |
| 123 | +}) |
| 124 | + |
| 125 | +t.Run("OrgIDOnlyError",func(t*testing.T) { |
| 126 | +cfg:=testAccGroupDataSourceConfig{ |
| 127 | +URL:client.URL.String(), |
| 128 | +Token:client.SessionToken(), |
| 129 | +OrganizationID:PtrTo(firstUser.OrganizationIDs[0].String()), |
| 130 | +} |
| 131 | +resource.Test(t, resource.TestCase{ |
| 132 | +PreCheck:func() {testAccPreCheck(t) }, |
| 133 | +ProtoV6ProviderFactories:testAccProtoV6ProviderFactories, |
| 134 | +// Neither ID nor Username |
| 135 | +Steps: []resource.TestStep{ |
| 136 | +{ |
| 137 | +Config:cfg.String(t), |
| 138 | +ExpectError:regexp.MustCompile(`At least one attribute out of \[name,id\] must be specified`), |
| 139 | +}, |
| 140 | +}, |
| 141 | +}) |
| 142 | +}) |
| 143 | + |
| 144 | +t.Run("NoneError",func(t*testing.T) { |
| 145 | +cfg:=testAccGroupDataSourceConfig{ |
| 146 | +URL:client.URL.String(), |
| 147 | +Token:client.SessionToken(), |
| 148 | +} |
| 149 | +resource.Test(t, resource.TestCase{ |
| 150 | +PreCheck:func() {testAccPreCheck(t) }, |
| 151 | +ProtoV6ProviderFactories:testAccProtoV6ProviderFactories, |
| 152 | +// Neither ID nor Username |
| 153 | +Steps: []resource.TestStep{ |
| 154 | +{ |
| 155 | +Config:cfg.String(t), |
| 156 | +ExpectError:regexp.MustCompile(`At least one attribute out of \[name,id\] must be specified`), |
| 157 | +}, |
| 158 | +}, |
| 159 | +}) |
| 160 | +}) |
| 161 | +} |
| 162 | + |
| 163 | +typetestAccGroupDataSourceConfigstruct { |
| 164 | +URLstring |
| 165 | +Tokenstring |
| 166 | + |
| 167 | +ID*string |
| 168 | +Name*string |
| 169 | +OrganizationID*string |
| 170 | +} |
| 171 | + |
| 172 | +func (ctestAccGroupDataSourceConfig)String(t*testing.T)string { |
| 173 | +tpl:=` |
| 174 | +provider coderd { |
| 175 | +url = "{{.URL}}" |
| 176 | +token = "{{.Token}}" |
| 177 | +} |
| 178 | +
|
| 179 | +data "coderd_group" "test" { |
| 180 | +id = {{orNull .ID}} |
| 181 | +name = {{orNull .Name}} |
| 182 | +organization_id = {{orNull .OrganizationID}} |
| 183 | +} |
| 184 | +` |
| 185 | + |
| 186 | +funcMap:= template.FuncMap{ |
| 187 | +"orNull":PrintOrNull(t), |
| 188 | +} |
| 189 | + |
| 190 | +buf:= strings.Builder{} |
| 191 | +tmpl,err:=template.New("groupDataSource").Funcs(funcMap).Parse(tpl) |
| 192 | +require.NoError(t,err) |
| 193 | + |
| 194 | +err=tmpl.Execute(&buf,c) |
| 195 | +require.NoError(t,err) |
| 196 | +returnbuf.String() |
| 197 | +} |