Skip to content

Increase test coverage #332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package activateserviceaccount
import (
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/zalando/go-keyring"

"github.com/google/go-cmp/cmp"
)
Expand Down Expand Up @@ -120,3 +122,62 @@ func TestParseInput(t *testing.T) {
})
}
}

func TestStoreFlags(t *testing.T) {
tests := []struct {
description string
model *inputModel
isValid bool
}{
{
description: "base",
model: fixtureInputModel(),
isValid: true,
},
{
description: "no values",
model: &inputModel{
ServiceAccountToken: "",
ServiceAccountKeyPath: "",
PrivateKeyPath: "",
TokenCustomEndpoint: "",
JwksCustomEndpoint: "",
},
isValid: true,
},
}

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
// Initialize an empty keyring
keyring.MockInit()

err := storeFlags(tt.model)
if !tt.isValid {
if err == nil {
t.Fatalf("did not fail on invalid input")
}
return
}
if err != nil {
t.Fatalf("store flags: %v", err)
}

value, err := auth.GetAuthField(auth.TOKEN_CUSTOM_ENDPOINT)
if err != nil {
t.Errorf("Failed to get value of auth field: %v", err)
}
if value != tt.model.TokenCustomEndpoint {
t.Errorf("Value of \"%s\" does not match: expected \"%s\", got \"%s\"", auth.TOKEN_CUSTOM_ENDPOINT, tt.model.TokenCustomEndpoint, value)
}

value, err = auth.GetAuthField(auth.JWKS_CUSTOM_ENDPOINT)
if err != nil {
t.Errorf("Failed to get value of auth field: %v", err)
}
if value != tt.model.JwksCustomEndpoint {
t.Errorf("Value of \"%s\" does not match: expected \"%s\", got \"%s\"", auth.JWKS_CUSTOM_ENDPOINT, tt.model.TokenCustomEndpoint, value)
}
})
}
}
75 changes: 75 additions & 0 deletions internal/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cmd

import (
"errors"
"testing"

"github.com/spf13/cobra"
pkgErrors "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
)

var cmd *cobra.Command
var service *cobra.Command
var resource *cobra.Command
var operation *cobra.Command

func setupCmd() {
cmd = &cobra.Command{
Use: "stackit",
}
service = &cobra.Command{
Use: "service",
}
resource = &cobra.Command{
Use: "resource",
}
operation = &cobra.Command{
Use: "operation",
}
cmd.AddCommand(service)
service.AddCommand(resource)
resource.AddCommand(operation)
}

func TestBeautifyUnknownAndMissingCommandsError(t *testing.T) {
tests := []struct {
description string
inputError error
command *cobra.Command
expectedMsg string
isNotUnknownFlagError bool
}{
{
description: "root command, extra input is a flag",
inputError: errors.New("unknown flag: --something"),
command: cmd,
expectedMsg: pkgErrors.SUBCOMMAND_MISSING,
},
{
description: "non unknown flag error, return the same",
inputError: errors.New("some error"),
command: cmd,
expectedMsg: "some error",
isNotUnknownFlagError: true,
},
}

setupCmd()
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
actualError := beautifyUnknownAndMissingCommandsError(cmd, tt.inputError)

if tt.isNotUnknownFlagError {
if actualError.Error() != tt.expectedMsg {
t.Fatalf("expected error message to be %s, got %s", tt.expectedMsg, actualError.Error())
}
return
}

appendedErr := pkgErrors.AppendUsageTip(errors.New(tt.expectedMsg), cmd)
if actualError.Error() != appendedErr.Error() {
t.Fatalf("expected error to be %s, got %s", appendedErr.Error(), actualError.Error())
}
})
}
}
71 changes: 71 additions & 0 deletions internal/pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package config

import (
"os"
"path/filepath"
"testing"

"github.com/spf13/viper"
)

func TestWrite(t *testing.T) {
tests := []struct {
description string
folderName string
folderExists bool
}{
{
description: "write config file",
folderName: "",
},
{
description: "write config file to new folder",
folderName: "new-folder",
},
{
description: "write config file to existing folder",
folderName: "existing-folder",
folderExists: true,
},
}

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
configPath := filepath.Join(os.TempDir(), tt.folderName, "config.json")
viper.SetConfigFile(configPath)
folderPath = filepath.Dir(configPath)

if tt.folderExists {
err := os.MkdirAll(folderPath, os.ModePerm)
if err != nil {
t.Fatalf("expected error to be nil, got %v", err)
}
}

err := Write()
if err != nil {
t.Fatalf("expected error to be nil, got %v", err)
}

// Check if the file was created
_, err = os.Stat(configPath)
if os.IsNotExist(err) {
t.Fatalf("expected file to exist, got %v", err)
}

// Delete the file
err = os.Remove(configPath)
if err != nil {
t.Fatalf("expected error to be nil, got %v", err)
}

// Delete the folder
if tt.folderName != "" {
err = os.Remove(folderPath)
if err != nil {
t.Fatalf("expected error to be nil, got %v", err)
}
}
})
}
}
Loading