Skip to content

Commit d86e97b

Browse files
Increase test coverage (#332)
* add testing * more testing * fix linting * Update internal/cmd/root_test.go Co-authored-by: Vicente Pinto <vicente.pinto@freiheit.com> * address PR comments --------- Co-authored-by: Vicente Pinto <vicente.pinto@freiheit.com>
1 parent d3f5282 commit d86e97b

File tree

8 files changed

+1022
-0
lines changed

8 files changed

+1022
-0
lines changed

internal/cmd/auth/activate-service-account/activate_service_account_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package activateserviceaccount
33
import (
44
"testing"
55

6+
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
67
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
78
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
9+
"github.com/zalando/go-keyring"
810

911
"github.com/google/go-cmp/cmp"
1012
)
@@ -120,3 +122,62 @@ func TestParseInput(t *testing.T) {
120122
})
121123
}
122124
}
125+
126+
func TestStoreFlags(t *testing.T) {
127+
tests := []struct {
128+
description string
129+
model *inputModel
130+
isValid bool
131+
}{
132+
{
133+
description: "base",
134+
model: fixtureInputModel(),
135+
isValid: true,
136+
},
137+
{
138+
description: "no values",
139+
model: &inputModel{
140+
ServiceAccountToken: "",
141+
ServiceAccountKeyPath: "",
142+
PrivateKeyPath: "",
143+
TokenCustomEndpoint: "",
144+
JwksCustomEndpoint: "",
145+
},
146+
isValid: true,
147+
},
148+
}
149+
150+
for _, tt := range tests {
151+
t.Run(tt.description, func(t *testing.T) {
152+
// Initialize an empty keyring
153+
keyring.MockInit()
154+
155+
err := storeFlags(tt.model)
156+
if !tt.isValid {
157+
if err == nil {
158+
t.Fatalf("did not fail on invalid input")
159+
}
160+
return
161+
}
162+
if err != nil {
163+
t.Fatalf("store flags: %v", err)
164+
}
165+
166+
value, err := auth.GetAuthField(auth.TOKEN_CUSTOM_ENDPOINT)
167+
if err != nil {
168+
t.Errorf("Failed to get value of auth field: %v", err)
169+
}
170+
if value != tt.model.TokenCustomEndpoint {
171+
t.Errorf("Value of \"%s\" does not match: expected \"%s\", got \"%s\"", auth.TOKEN_CUSTOM_ENDPOINT, tt.model.TokenCustomEndpoint, value)
172+
}
173+
174+
value, err = auth.GetAuthField(auth.JWKS_CUSTOM_ENDPOINT)
175+
if err != nil {
176+
t.Errorf("Failed to get value of auth field: %v", err)
177+
}
178+
if value != tt.model.JwksCustomEndpoint {
179+
t.Errorf("Value of \"%s\" does not match: expected \"%s\", got \"%s\"", auth.JWKS_CUSTOM_ENDPOINT, tt.model.TokenCustomEndpoint, value)
180+
}
181+
})
182+
}
183+
}

internal/cmd/root_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/spf13/cobra"
8+
pkgErrors "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
9+
)
10+
11+
var cmd *cobra.Command
12+
var service *cobra.Command
13+
var resource *cobra.Command
14+
var operation *cobra.Command
15+
16+
func setupCmd() {
17+
cmd = &cobra.Command{
18+
Use: "stackit",
19+
}
20+
service = &cobra.Command{
21+
Use: "service",
22+
}
23+
resource = &cobra.Command{
24+
Use: "resource",
25+
}
26+
operation = &cobra.Command{
27+
Use: "operation",
28+
}
29+
cmd.AddCommand(service)
30+
service.AddCommand(resource)
31+
resource.AddCommand(operation)
32+
}
33+
34+
func TestBeautifyUnknownAndMissingCommandsError(t *testing.T) {
35+
tests := []struct {
36+
description string
37+
inputError error
38+
command *cobra.Command
39+
expectedMsg string
40+
isNotUnknownFlagError bool
41+
}{
42+
{
43+
description: "root command, extra input is a flag",
44+
inputError: errors.New("unknown flag: --something"),
45+
command: cmd,
46+
expectedMsg: pkgErrors.SUBCOMMAND_MISSING,
47+
},
48+
{
49+
description: "non unknown flag error, return the same",
50+
inputError: errors.New("some error"),
51+
command: cmd,
52+
expectedMsg: "some error",
53+
isNotUnknownFlagError: true,
54+
},
55+
}
56+
57+
setupCmd()
58+
for _, tt := range tests {
59+
t.Run(tt.description, func(t *testing.T) {
60+
actualError := beautifyUnknownAndMissingCommandsError(cmd, tt.inputError)
61+
62+
if tt.isNotUnknownFlagError {
63+
if actualError.Error() != tt.expectedMsg {
64+
t.Fatalf("expected error message to be %s, got %s", tt.expectedMsg, actualError.Error())
65+
}
66+
return
67+
}
68+
69+
appendedErr := pkgErrors.AppendUsageTip(errors.New(tt.expectedMsg), cmd)
70+
if actualError.Error() != appendedErr.Error() {
71+
t.Fatalf("expected error to be %s, got %s", appendedErr.Error(), actualError.Error())
72+
}
73+
})
74+
}
75+
}

internal/pkg/config/config_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/spf13/viper"
9+
)
10+
11+
func TestWrite(t *testing.T) {
12+
tests := []struct {
13+
description string
14+
folderName string
15+
folderExists bool
16+
}{
17+
{
18+
description: "write config file",
19+
folderName: "",
20+
},
21+
{
22+
description: "write config file to new folder",
23+
folderName: "new-folder",
24+
},
25+
{
26+
description: "write config file to existing folder",
27+
folderName: "existing-folder",
28+
folderExists: true,
29+
},
30+
}
31+
32+
for _, tt := range tests {
33+
t.Run(tt.description, func(t *testing.T) {
34+
configPath := filepath.Join(os.TempDir(), tt.folderName, "config.json")
35+
viper.SetConfigFile(configPath)
36+
folderPath = filepath.Dir(configPath)
37+
38+
if tt.folderExists {
39+
err := os.MkdirAll(folderPath, os.ModePerm)
40+
if err != nil {
41+
t.Fatalf("expected error to be nil, got %v", err)
42+
}
43+
}
44+
45+
err := Write()
46+
if err != nil {
47+
t.Fatalf("expected error to be nil, got %v", err)
48+
}
49+
50+
// Check if the file was created
51+
_, err = os.Stat(configPath)
52+
if os.IsNotExist(err) {
53+
t.Fatalf("expected file to exist, got %v", err)
54+
}
55+
56+
// Delete the file
57+
err = os.Remove(configPath)
58+
if err != nil {
59+
t.Fatalf("expected error to be nil, got %v", err)
60+
}
61+
62+
// Delete the folder
63+
if tt.folderName != "" {
64+
err = os.Remove(folderPath)
65+
if err != nil {
66+
t.Fatalf("expected error to be nil, got %v", err)
67+
}
68+
}
69+
})
70+
}
71+
}

0 commit comments

Comments
 (0)