terraform-provider-stackitp.../stackit/internal/services/modelserving/token/resource_test.go
Patrick Koss 435de4c9eb
feat: add model serving resource
* add model serving

* add right provider config

* rename model_serving to modelserving

* add model serving custom endpoint everywhere

* rename file

* add default region, docs for model serving

* add right order of wait handler

* rotate after to token

* fixes

* add initial doc files

* address code comments

* refactor region description

* remove warning for not found resources

* add service enablement

* address code comments

* address code comments

* fix datasource

* fix acc test

* review changes

* review changes

* review changes

* review changes

* review changes

* review changes

* review changes

* review changes

* review changes

* embed markdown description

* go tidy

---------

Co-authored-by: Mauritz Uphoff <mauritz.uphoff@me.com>
Co-authored-by: Mauritz Uphoff <39736813+h3adex@users.noreply.github.com>
2025-03-28 16:20:25 +01:00

341 lines
8.6 KiB
Go

package token
import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
"github.com/stackitcloud/stackit-sdk-go/services/modelserving"
)
func TestMapGetTokenFields(t *testing.T) {
t.Parallel()
tests := []struct {
description string
state *Model
input *modelserving.GetTokenResponse
expected Model
isValid bool
}{
{
description: "should error when response is nil",
state: &Model{},
input: nil,
expected: Model{},
isValid: false,
},
{
description: "should error when token is nil in response",
state: &Model{},
input: &modelserving.GetTokenResponse{Token: nil},
expected: Model{},
isValid: false,
},
{
description: "should error when state is nil in response",
state: nil,
input: &modelserving.GetTokenResponse{
Token: &modelserving.Token{},
},
expected: Model{},
isValid: false,
},
{
description: "should map fields correctly",
state: &Model{
Id: types.StringValue("pid,eu01,tid"),
ProjectId: types.StringValue("pid"),
TokenId: types.StringValue("tid"),
Region: types.StringValue("eu01"),
RotateWhenChanged: types.MapNull(types.StringType),
},
input: &modelserving.GetTokenResponse{
Token: &modelserving.Token{
Id: utils.Ptr("tid"),
ValidUntil: utils.Ptr(
time.Date(2099, 1, 1, 0, 0, 0, 0, time.UTC),
),
State: utils.Ptr("active"),
Name: utils.Ptr("name"),
Description: utils.Ptr("desc"),
Region: utils.Ptr("eu01"),
},
},
expected: Model{
Id: types.StringValue("pid,eu01,tid"),
ProjectId: types.StringValue("pid"),
Region: types.StringValue("eu01"),
TokenId: types.StringValue("tid"),
Name: types.StringValue("name"),
Description: types.StringValue("desc"),
State: types.StringValue("active"),
ValidUntil: types.StringValue("2099-01-01T00:00:00Z"),
RotateWhenChanged: types.MapNull(types.StringType),
},
isValid: true,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
t.Parallel()
err := mapGetResponse(tt.input, tt.state)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(tt.state, &tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
})
}
}
func TestMapCreateTokenFields(t *testing.T) {
t.Parallel()
tests := []struct {
description string
state *Model
inputCreateTokenResponse *modelserving.CreateTokenResponse
inputGetTokenResponse *modelserving.GetTokenResponse
expected Model
isValid bool
}{
{
description: "should error when create token response is nil",
state: &Model{},
inputCreateTokenResponse: nil,
inputGetTokenResponse: nil,
expected: Model{},
isValid: false,
},
{
description: "should error when token is nil in create token response",
state: &Model{},
inputCreateTokenResponse: &modelserving.CreateTokenResponse{
Token: nil,
},
inputGetTokenResponse: nil,
expected: Model{},
isValid: false,
},
{
description: "should error when get token response is nil",
state: &Model{},
inputCreateTokenResponse: &modelserving.CreateTokenResponse{
Token: &modelserving.TokenCreated{},
},
inputGetTokenResponse: nil,
expected: Model{},
isValid: false,
},
{
description: "should error when get token response is nil",
state: &Model{
Id: types.StringValue("pid,eu01,tid"),
ProjectId: types.StringValue("pid"),
},
inputCreateTokenResponse: &modelserving.CreateTokenResponse{
Token: &modelserving.TokenCreated{
Id: utils.Ptr("tid"),
ValidUntil: utils.Ptr(
time.Date(2099, 1, 1, 0, 0, 0, 0, time.UTC),
),
State: utils.Ptr("active"),
Name: utils.Ptr("name"),
Description: utils.Ptr("desc"),
Region: utils.Ptr("eu01"),
Content: utils.Ptr("content"),
},
},
inputGetTokenResponse: nil,
expected: Model{},
isValid: false,
},
{
description: "should map fields correctly",
state: &Model{
Id: types.StringValue("pid,eu01,tid"),
ProjectId: types.StringValue("pid"),
Region: types.StringValue("eu01"),
RotateWhenChanged: types.MapNull(types.StringType),
},
inputCreateTokenResponse: &modelserving.CreateTokenResponse{
Token: &modelserving.TokenCreated{
Id: utils.Ptr("tid"),
ValidUntil: utils.Ptr(
time.Date(2099, 1, 1, 0, 0, 0, 0, time.UTC),
),
State: utils.Ptr("active"),
Name: utils.Ptr("name"),
Description: utils.Ptr("desc"),
Region: utils.Ptr("eu01"),
Content: utils.Ptr("content"),
},
},
inputGetTokenResponse: &modelserving.GetTokenResponse{
Token: &modelserving.Token{
State: utils.Ptr("active"),
},
},
expected: Model{
Id: types.StringValue("pid,eu01,tid"),
ProjectId: types.StringValue("pid"),
Region: types.StringValue("eu01"),
TokenId: types.StringValue("tid"),
Name: types.StringValue("name"),
Description: types.StringValue("desc"),
State: types.StringValue("active"),
ValidUntil: types.StringValue("2099-01-01T00:00:00Z"),
Token: types.StringValue("content"),
RotateWhenChanged: types.MapNull(types.StringType),
},
isValid: true,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
t.Parallel()
err := mapCreateResponse(
tt.inputCreateTokenResponse,
tt.inputGetTokenResponse,
tt.state,
"eu01",
)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(tt.state, &tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
})
}
}
func TestToCreatePayload(t *testing.T) {
t.Parallel()
tests := []struct {
description string
input *Model
expected *modelserving.CreateTokenPayload
isValid bool
}{
{
description: "should error on nil input",
input: nil,
expected: nil,
isValid: false,
},
{
description: "should convert correctly",
input: &Model{
Name: types.StringValue("name"),
Description: types.StringValue("desc"),
TTLDuration: types.StringValue("1h"),
},
expected: &modelserving.CreateTokenPayload{
Name: utils.Ptr("name"),
Description: utils.Ptr("desc"),
TtlDuration: utils.Ptr("1h"),
},
isValid: true,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
t.Parallel()
output, err := toCreatePayload(tt.input)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(output, tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
})
}
}
func TestToUpdatePayload(t *testing.T) {
t.Parallel()
tests := []struct {
description string
input *Model
expected *modelserving.PartialUpdateTokenPayload
isValid bool
}{
{
description: "should error on nil input",
input: nil,
expected: nil,
isValid: false,
},
{
description: "should convert correctly",
input: &Model{
Name: types.StringValue("name"),
Description: types.StringValue("desc"),
},
expected: &modelserving.PartialUpdateTokenPayload{
Name: utils.Ptr("name"),
Description: utils.Ptr("desc"),
},
isValid: true,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
t.Parallel()
output, err := toUpdatePayload(tt.input)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(output, tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
})
}
}