Skip to content
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

chore: increase test coverage #151

Merged
merged 5 commits into from
Jun 27, 2023
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
105 changes: 105 additions & 0 deletions cmd/argo-watcher/argo_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package main

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"

"github.com/stretchr/testify/assert"

"github.com/shini4i/argo-watcher/internal/models"
)

var (
mux = http.NewServeMux()
server *httptest.Server
api *ArgoApi

userinfo = models.Userinfo{
Username: "test",
LoggedIn: true,
}
)

func getUserInfoHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
_, err := w.Write([]byte(`Method not allowed`))
if err != nil {
fmt.Println(err)
}
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

if err := json.NewEncoder(w).Encode(userinfo); err != nil {
fmt.Println("error encoding userinfo")
panic(err)
}
}

func getApplicationHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
_, err := w.Write([]byte(`Method not allowed`))
if err != nil {
fmt.Println(err)
}
return
}

var application models.Application
application.Status.Health.Status = "Healthy"
application.Status.Sync.Status = "Synced"
application.Status.Summary.Images = []string{"example.com/image:v0.1.0", "example.com/image:v0.1.1"}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

if err := json.NewEncoder(w).Encode(application); err != nil {
fmt.Println("error encoding application")
panic(err)
}
}

func init() {
mux.HandleFunc("/api/v1/session/userinfo", getUserInfoHandler)
mux.HandleFunc("/api/v1/applications/test", getApplicationHandler)
server = httptest.NewServer(mux)
api = &ArgoApi{baseUrl: server.URL, client: server.Client()}
}

func TestArgoApi_GetUserInfo(t *testing.T) {
receivedUserinfo, err := api.GetUserInfo()
if err != nil {
t.Error(err)
}

if !reflect.DeepEqual(receivedUserinfo, &userinfo) {
t.Errorf("Expected: %v, received: %v", userinfo, receivedUserinfo)
}
}

func TestArgoApi_GetApplication(t *testing.T) {
app, err := api.GetApplication("test")
if err != nil {
t.Error(err)
}

if !assert.Equal(t, app.Status.Health.Status, "Healthy") {
t.Errorf("Expected: %v, received: %v", "Healthy", app.Status.Health.Status)
}

if !assert.Equal(t, app.Status.Sync.Status, "Synced") {
t.Errorf("Expected: %v, received: %v", "Synced", app.Status.Sync.Status)
}

if !assert.Equal(t, app.Status.Summary.Images, []string{"example.com/image:v0.1.0", "example.com/image:v0.1.1"}) {
t.Errorf("Expected: %v, received: %v", []string{"example.com/image:v0.1.0", "example.com/image:v0.1.1"}, app.Status.Summary.Images)
}
}
71 changes: 71 additions & 0 deletions cmd/argo-watcher/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package config

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewServerConfig(t *testing.T) {
// Set up the required environment variables
if err := os.Setenv("ARGO_URL", "https://example.com"); err != nil {
t.Fatal(err)
}

if err := os.Setenv("ARGO_TOKEN", "secret-token"); err != nil {
t.Fatal(err)
}

if err := os.Setenv("STATE_TYPE", "postgres"); err != nil {
t.Fatal(err)
}

// Cleanup the environment variables after the test
defer func() {
if err := os.Unsetenv("ARGO_URL"); err != nil {
t.Fatal(err)
}

if err := os.Unsetenv("ARGO_TOKEN"); err != nil {
t.Fatal(err)
}

if err := os.Unsetenv("STATE_TYPE"); err != nil {
t.Fatal(err)
}
}()

// Call the NewServerConfig function
cfg, err := NewServerConfig()

// Assert that the configuration was parsed successfully
assert.NoError(t, err)
assert.NotNil(t, cfg)

// Assert specific field values
assert.Equal(t, "https://example.com", cfg.ArgoUrl)
assert.Equal(t, "secret-token", cfg.ArgoToken)
assert.Equal(t, "postgres", cfg.StateType)
}

func TestNewServerConfig_RequiredFieldsMissing(t *testing.T) {
// Set up environment variables with missing required fields
if err := os.Setenv("ARGO_URL", "https://example.com"); err != nil {
t.Fatal(err)
}

// Cleanup the environment variables after the test
defer func() {
if err := os.Unsetenv("ARGO_URL"); err != nil {
t.Fatal(err)
}
}()

// Call the NewServerConfig function
cfg, err := NewServerConfig()

// Assert that an error is returned due to missing required fields
assert.Error(t, err)
assert.Nil(t, cfg)
}