From f5cef183ccbed738b0d7cdbd97a8327a1f9d68a3 Mon Sep 17 00:00:00 2001 From: ShifnaZarnaz Date: Thu, 6 Jun 2024 17:39:42 +0530 Subject: [PATCH 1/5] Updated the test files --- cmd/main_test.go | 16 ++ go.mod | 16 +- go.sum | 65 -------- pkg/agent/agent_test.go | 323 ++++++++++++++++++++++++++++++-------- pkg/config/config_test.go | 130 +++++++++++++++ 5 files changed, 405 insertions(+), 145 deletions(-) create mode 100644 cmd/main_test.go create mode 100644 pkg/config/config_test.go diff --git a/cmd/main_test.go b/cmd/main_test.go new file mode 100644 index 0000000..35231c6 --- /dev/null +++ b/cmd/main_test.go @@ -0,0 +1,16 @@ +package main + +import "testing" + +func Test_main(t *testing.T) { + tests := []struct { + name string + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + main() + }) + } +} diff --git a/go.mod b/go.mod index 803d704..45126c9 100644 --- a/go.mod +++ b/go.mod @@ -41,7 +41,6 @@ require ( github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect github.com/beorn7/perks v1.0.1 // indirect - github.com/cenkalti/backoff/v3 v3.0.0 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/chai2010/gettext-go v1.0.2 // indirect github.com/cloudflare/circl v1.3.7 // indirect @@ -62,7 +61,6 @@ require ( github.com/felixge/httpsnoop v1.0.3 // indirect github.com/go-errors/errors v1.4.2 // indirect github.com/go-gorp/gorp/v3 v3.1.0 // indirect - github.com/go-jose/go-jose/v3 v3.0.1 // indirect github.com/go-logr/logr v1.3.0 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-openapi/jsonpointer v0.19.6 // indirect @@ -84,20 +82,10 @@ require ( github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect - github.com/hashicorp/go-retryablehttp v0.7.2 // indirect - github.com/hashicorp/go-rootcerts v1.0.2 // indirect - github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6 // indirect - github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect - github.com/hashicorp/go-sockaddr v1.0.2 // indirect - github.com/hashicorp/hcl v1.0.0 // indirect github.com/hashicorp/terraform-json v0.15.0 // indirect - github.com/hashicorp/vault/api v1.9.2 // indirect - github.com/hashicorp/vault/api/auth/kubernetes v0.4.1 // indirect github.com/huandu/xstrings v1.4.0 // indirect github.com/imdario/mergo v0.3.15 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/intelops/go-common v1.0.3 // indirect - github.com/intelops/vault-cred v1.0.3 // indirect github.com/jmoiron/sqlx v1.3.5 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect @@ -113,9 +101,7 @@ require ( github.com/mattn/go-runewidth v0.0.9 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect - github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/go-wordwrap v1.0.1 // indirect - github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/moby/locker v1.0.1 // indirect github.com/moby/spdystream v0.2.0 // indirect @@ -138,10 +124,10 @@ require ( github.com/rubenv/sql-migrate v1.5.2 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/ryanuber/columnize v2.1.0+incompatible // indirect - github.com/ryanuber/go-glob v1.0.0 // indirect github.com/shopspring/decimal v1.3.1 // indirect github.com/spf13/cast v1.5.1 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/stretchr/objx v0.5.0 // indirect github.com/titanous/rocacheck v0.0.0-20171023193734-afe73141d399 // indirect github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect diff --git a/go.sum b/go.sum index b6143f0..3b36b1f 100644 --- a/go.sum +++ b/go.sum @@ -69,7 +69,6 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk= github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw= github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo= -github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= @@ -79,7 +78,6 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bshuster-repo/logrus-logstash-hook v1.0.0 h1:e+C0SB5R1pu//O4MQ3f9cFuPGoOVeF2fE4Og9otCc70= github.com/bshuster-repo/logrus-logstash-hook v1.0.0/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk= github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd h1:rFt+Y/IK1aEZkEHchZRSq9OQbsSzIT/OrI8YFFmRIng= @@ -89,8 +87,6 @@ github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b/go.mod h1:obH5gd0Bsq github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0 h1:nvj0OLI3YqYXer/kZD8Ri1aaunCxIEsOst1BVJswV0o= github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE= github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= -github.com/cenkalti/backoff/v3 v3.0.0 h1:ske+9nBpD9qZsTBoF41nW5L+AIuFBKMeze18XQ3eG1c= -github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cert-manager/cert-manager v1.12.2 h1:lJ7Xn0VhmBA4uOZb5dlSZzepu38ez73okOqgE24x8YM= github.com/cert-manager/cert-manager v1.12.2/go.mod h1:ql0msU88JCcQSceN+PFjEY8U+AMe13y06vO2klJk8bs= @@ -159,7 +155,6 @@ github.com/evanphx/json-patch v5.7.0+incompatible h1:vgGkfT/9f8zE6tvSCe74nfpAVDQ github.com/evanphx/json-patch v5.7.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d h1:105gxyaGwCFad8crR9dcMQWvV9Hvulu6hwUh4tWPJnM= github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d/go.mod h1:ZZMPRZwes7CROmyNKgQzC3XPs6L/G2EJLHddWejkmf4= -github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk= @@ -185,9 +180,6 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2 github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gorp/gorp/v3 v3.1.0 h1:ItKF/Vbuj31dmV4jxA1qblpSwkl9g1typ24xoe70IGs= github.com/go-gorp/gorp/v3 v3.1.0/go.mod h1:dLEjIyyRNiXvNZ8PSmzpt1GsWAUK8kjVhEpjH8TixEw= -github.com/go-jose/go-jose/v3 v3.0.0/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8= -github.com/go-jose/go-jose/v3 v3.0.1 h1:pWmKFVtt+Jl0vBZTIpz/eAKwsm6LkIxDVVbFHKkchhA= -github.com/go-jose/go-jose/v3 v3.0.1/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= @@ -216,7 +208,6 @@ github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LB github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= -github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/gobuffalo/logger v1.0.6 h1:nnZNpxYo0zx+Aj9RfMPBm+x9zAU2OayFh/xrAWi34HU= github.com/gobuffalo/logger v1.0.6/go.mod h1:J31TBEHR1QLV2683OXTAItYIg8pv2JMHnF/quuAbMjs= github.com/gobuffalo/packd v1.0.1 h1:U2wXfRr4E9DH8IdsDLlRFwTZTK7hLfq9qT/QHXGVe/0= @@ -273,7 +264,6 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= @@ -315,26 +305,10 @@ github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.1/go.mod h1:w9Y7gY31krpLmrV github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= -github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= -github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= -github.com/hashicorp/go-hclog v0.16.2/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= -github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= -github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY= -github.com/hashicorp/go-retryablehttp v0.7.2 h1:AcYqCvkpalPnPF2pn0KamgwamS42TqUDDYFRKq/RAd0= -github.com/hashicorp/go-retryablehttp v0.7.2/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8= -github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc= -github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8= -github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6 h1:om4Al8Oy7kCm/B86rLCLah4Dt5Aa0Fr5rYBG60OzwHQ= -github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6/go.mod h1:QmrqtbKuxxSWTN3ETMPuB+VtEiBJ/A9XhoYGv8E1uD8= -github.com/hashicorp/go-secure-stdlib/strutil v0.1.1/go.mod h1:gKOamz3EwoIoJq7mlMIRBpVTAUn8qPCrEclOKKWhD3U= -github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 h1:kes8mmyCpxJsI7FTwtzRqEy9CdjCtrXrXGuOpxEA7Ts= -github.com/hashicorp/go-secure-stdlib/strutil v0.1.2/go.mod h1:Gou2R9+il93BqX25LAKCLuM+y9U2T4hlwvT1yprcna4= -github.com/hashicorp/go-sockaddr v1.0.2 h1:ztczhD1jLxIRjVejw8gFomI1BQZOe2WoVOu0SyteCQc= -github.com/hashicorp/go-sockaddr v1.0.2/go.mod h1:rB4wwRAUzs07qva3c5SdrY/NEtAUjGlgmH/UkBUC97A= github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek= github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= @@ -343,16 +317,10 @@ github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+l github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/hc-install v0.5.2 h1:SfwMFnEXVVirpwkDuSF5kymUOhrUxrTq3udEseZdOD0= github.com/hashicorp/hc-install v0.5.2/go.mod h1:9QISwe6newMWIfEiXpzuu1k9HAGtQYgnSH8H9T8wmoI= -github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= -github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/terraform-exec v0.18.1 h1:LAbfDvNQU1l0NOQlTuudjczVhHj061fNX5H8XZxHlH4= github.com/hashicorp/terraform-exec v0.18.1/go.mod h1:58wg4IeuAJ6LVsLUeD2DWZZoc/bYi6dzhLHzxM41980= github.com/hashicorp/terraform-json v0.15.0 h1:/gIyNtR6SFw6h5yzlbDbACyGvIhKtQi8mTsbkNd79lE= github.com/hashicorp/terraform-json v0.15.0/go.mod h1:+L1RNzjDU5leLFZkHTFTbJXaoqUC6TqXlFgDoOXrtvk= -github.com/hashicorp/vault/api v1.9.2 h1:YjkZLJ7K3inKgMZ0wzCU9OHqc+UqMQyXsPXnf3Cl2as= -github.com/hashicorp/vault/api v1.9.2/go.mod h1:jo5Y/ET+hNyz+JnKDt8XLAdKs+AM0G5W0Vp1IrFI8N8= -github.com/hashicorp/vault/api/auth/kubernetes v0.4.1 h1:amFWL1ZhwMWdmqvT51J9phXu835kY25wFfTrY/3yXd0= -github.com/hashicorp/vault/api/auth/kubernetes v0.4.1/go.mod h1:ikWDT8Adnfvm+8DzKez50vvLD9GWD/unZfJxeqP09sU= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU= @@ -364,10 +332,6 @@ github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM= github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/intelops/go-common v1.0.3 h1:j8zus9VT/T3zIIyUI2lexy0IrRwhD1KjUL/TGGmnD8A= -github.com/intelops/go-common v1.0.3/go.mod h1:8xudyk2dSOtaXZ+7/evy0dPX7oSR2O4v8uLpT90O/kA= -github.com/intelops/vault-cred v1.0.3 h1:mnlTYh3XfWTCZ5d9HXqMnMjaJc6isSDONhmpxvJg/yY= -github.com/intelops/vault-cred v1.0.3/go.mod h1:IPDdys+Lf0lnvyu5mbtH2MMIYbaODylmp5xGnFRpOSg= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jmhodges/clock v1.2.0 h1:eq4kys+NI0PLngzaHEe7AmPT90XMGIEySD1JfV1PDIs= @@ -430,15 +394,8 @@ github.com/markbates/oncer v1.0.0 h1:E83IaVAHygyndzPimgUYJjbshhDTALZyXxvk9FOlQRY github.com/markbates/oncer v1.0.0/go.mod h1:Z59JA581E9GP6w96jai+TGqafHPW+cPfRxz2aSZ0mcI= github.com/markbates/safe v1.0.1 h1:yjZkbvRM6IzKj9tlu/zMJLS0n/V351OZWRnF3QfaUxI= github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0= -github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= -github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= -github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= -github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= -github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= @@ -452,19 +409,12 @@ github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zk github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/miekg/dns v1.1.55 h1:GoQ4hpsj0nFLYe+bWiCToyrBEJXkQfOOIvFGFy0lEgo= github.com/miekg/dns v1.1.55/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY= -github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= -github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= -github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= -github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= -github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= -github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= @@ -522,7 +472,6 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/poy/onpar v1.1.2 h1:QaNrNiZx0+Nar5dLgTVp5mXkyoVFIbepjyEoGSnhbAY= github.com/poy/onpar v1.1.2/go.mod h1:6X8FLNoxyr9kkmnlqpK6LSoiOtrO6MICtWwEuWkLjzg= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= @@ -553,8 +502,6 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v2.1.0+incompatible h1:j1Wcmh8OrK4Q7GXY+V7SVSY8nUWQxHW5TkBe7YUl+2s= github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= -github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk= -github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc= github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4= github.com/secure-systems-lab/go-securesystemslib v0.8.0 h1:mr5An6X45Kb2nddcFlbmfHkLguCE9laoZCUzEEpIZXA= github.com/secure-systems-lab/go-securesystemslib v0.8.0/go.mod h1:UH2VZVuJfCYR8WgMlCU1uFsOUU+KeyrTWcSS73NBOzU= @@ -647,13 +594,11 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= -golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -717,8 +662,6 @@ golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= -golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -738,13 +681,11 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= -golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -756,11 +697,9 @@ golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -778,7 +717,6 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= @@ -786,7 +724,6 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8= golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -799,13 +736,11 @@ golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/pkg/agent/agent_test.go b/pkg/agent/agent_test.go index a48995e..75d64bf 100644 --- a/pkg/agent/agent_test.go +++ b/pkg/agent/agent_test.go @@ -4,17 +4,33 @@ import ( "capten/pkg/agent/pb/agentpb" "capten/pkg/agent/pb/vaultcredpb" "capten/pkg/config" + "crypto/tls" + "fmt" + "log" "os" - - //"reflect" + "path/filepath" + "strings" "testing" "github.com/stretchr/testify/assert" + "google.golang.org/grpc/credentials" ) func TestGetAgentClient(t *testing.T) { + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } + type args struct { - config config.CaptenConfig + config config.CaptenConfig + clusterconf config.CaptenClusterValues } tests := []struct { name string @@ -23,13 +39,19 @@ func TestGetAgentClient(t *testing.T) { wantErr bool }{ { + name: "Secure connection", args: args{ config: config.CaptenConfig{ - AgentSecure: true, - AgentHostName: "captenagent", - // DomainName: "com", - // CaptenAgentPort: "50051", + AgentSecure: true, + AgentHostName: "captenagent", + CertDirPath: "/" + presentdir + "/cert/", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + }, + clusterconf: config.CaptenClusterValues{ + DomainName: "aws.optimizor.app", }, }, wantErr: false, @@ -43,6 +65,9 @@ func TestGetAgentClient(t *testing.T) { // DomainName: "com", // CaptenAgentPort: "50051", }, + clusterconf: config.CaptenClusterValues{ + DomainName: "aws.optimizor.app", + }, }, wantErr: false, }, @@ -68,6 +93,17 @@ func TestGetAgentClient(t *testing.T) { } func TestGetVaultClient(t *testing.T) { + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + dir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } + type args struct { config config.CaptenConfig } @@ -81,8 +117,12 @@ func TestGetVaultClient(t *testing.T) { name: "Secure connection", args: args{ config: config.CaptenConfig{ - AgentSecure: true, - AgentHostName: "captenagent", + AgentSecure: true, + AgentHostName: "captenagent", + CertDirPath: "/" + dir + "/cert/", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", }, }, wantErr: false, @@ -127,80 +167,233 @@ func TestGetVaultClient(t *testing.T) { } -func TestLoadTLSCredentials(t *testing.T) { - // Test case 1: LoadX509KeyPair fails - captenConfig := config.CaptenConfig{ - //CertDirPath: "/path/to/certs", - ClientCertFileName: "client.crt", - ClientKeyFileName: "client.key", - CAFileName: "ca.crt", - } - os.MkdirAll(captenConfig.CertDirPath, os.ModePerm) - defer os.RemoveAll(captenConfig.CertDirPath) +// func TestLoadTLSCredentials(t *testing.T) { +// // Test case 1: LoadX509KeyPair fails - _, err := loadTLSCredentials(captenConfig) - if err == nil { - t.Errorf("Expected error, got nil") - } +// currentdir, err := os.Getwd() +// if err != nil { +// log.Println("Error while getting cuerent dir", err) +// } +// dir, err := getRelativePathUpTo(currentdir) - // Test case 2: AppendCertsFromPEM fails - certFile := captenConfig.PrepareFilePath(captenConfig.CertDirPath, captenConfig.ClientCertFileName) - keyFile := captenConfig.PrepareFilePath(captenConfig.CertDirPath, captenConfig.ClientKeyFileName) - caFile := captenConfig.PrepareFilePath(captenConfig.CertDirPath, captenConfig.CAFileName) +// if err != nil { +// log.Println("Error while getting working dir", err) +// } - err = os.WriteFile(certFile, []byte("dummy cert"), os.ModePerm) - if err != nil { - t.Fatalf("Failed to write cert file: %v", err) - } +// captenConfig := config.CaptenConfig{ +// CertDirPath: "/" + dir + "/cert", +// ClientCertFileName: "client.crt", +// ClientKeyFileName: "client.key", +// CAFileName: "ca.crt", +// } +// os.MkdirAll(captenConfig.CertDirPath, os.ModePerm) +// defer os.RemoveAll(captenConfig.CertDirPath) - err = os.WriteFile(keyFile, []byte("dummy key"), os.ModePerm) - if err != nil { - t.Fatalf("Failed to write key file: %v", err) - } +// _, err = loadTLSCredentials(captenConfig) +// if err == nil { +// t.Errorf("Expected error, got nil") +// } - err = os.WriteFile(caFile, []byte("dummy ca"), os.ModePerm) - if err != nil { - t.Fatalf("Failed to write ca file: %v", err) - } +// // Test case 2: AppendCertsFromPEM fails +// certFile := captenConfig.PrepareFilePath(captenConfig.CertDirPath, captenConfig.ClientCertFileName) +// keyFile := captenConfig.PrepareFilePath(captenConfig.CertDirPath, captenConfig.ClientKeyFileName) +// caFile := captenConfig.PrepareFilePath(captenConfig.CertDirPath, captenConfig.CAFileName) - _, err = loadTLSCredentials(captenConfig) - if err == nil { - t.Errorf("Expected error, got nil") - } +// err = os.WriteFile(certFile, []byte("dummy cert"), os.ModePerm) +// if err != nil { +// t.Fatalf("Failed to write cert file: %v", err) +// } - // Test case 3: Successful load - certPEM, err := os.ReadFile("testdata/cert.pem") - if err != nil { - t.Fatalf("Failed to read cert file: %v", err) - } +// err = os.WriteFile(keyFile, []byte("dummy key"), os.ModePerm) +// if err != nil { +// t.Fatalf("Failed to write key file: %v", err) +// } - keyPEM, err := os.ReadFile("testdata/key.pem") - if err != nil { - t.Fatalf("Failed to read key file: %v", err) - } +// err = os.WriteFile(caFile, []byte("dummy ca"), os.ModePerm) +// if err != nil { +// t.Fatalf("Failed to write ca file: %v", err) +// } - caPEM, err := os.ReadFile("testdata/ca.pem") - if err != nil { - t.Fatalf("Failed to read ca file: %v", err) +// _, err = loadTLSCredentials(captenConfig) +// if err == nil { +// t.Errorf("Expected error, got nil") +// } + +// // Test case 3: Successful load +// certPEM, err := os.ReadFile("cert/cert.pem") +// if err != nil { +// t.Fatalf("Failed to read cert file: %v", err) +// } + +// keyPEM, err := os.ReadFile("cert/key.pem") +// if err != nil { +// t.Fatalf("Failed to read key file: %v", err) +// } + +// caPEM, err := os.ReadFile("cert/ca.pem") +// if err != nil { +// t.Fatalf("Failed to read ca file: %v", err) +// } + +// err = os.WriteFile(certFile, certPEM, os.ModePerm) +// if err != nil { +// t.Fatalf("Failed to write cert file: %v", err) +// } + +// err = os.WriteFile(keyFile, keyPEM, os.ModePerm) +// if err != nil { +// t.Fatalf("Failed to write key file: %v", err) +// } + +// err = os.WriteFile(caFile, caPEM, os.ModePerm) +// if err != nil { +// t.Fatalf("Failed to write ca file: %v", err) +// } + +// _, err = loadTLSCredentials(captenConfig) +// if err != nil { +// t.Errorf("Expected no error, got %v", err) +// } +// } + +func getRelativePathUpTo(currentPath string) (string, error) { + targetDir := "capten" + // Split the path into parts + parts := strings.Split(currentPath, string(filepath.Separator)) + + // Traverse the path parts and look for the target directory + for i, part := range parts { + if part == targetDir { + // Join the parts up to the target directory + return filepath.Join(parts[:i+1]...), nil + } } - err = os.WriteFile(certFile, certPEM, os.ModePerm) + return "", fmt.Errorf("directory %s not found in path %s", targetDir, currentPath) +} + +func Test_loadTLSCredentials(t *testing.T) { + + currentdir, err := os.Getwd() if err != nil { - t.Fatalf("Failed to write cert file: %v", err) + log.Println("Error while getting cuerent dir", err) } + dir, err := getRelativePathUpTo(currentdir) - err = os.WriteFile(keyFile, keyPEM, os.ModePerm) if err != nil { - t.Fatalf("Failed to write key file: %v", err) + log.Println("Error while getting working dir", err) } - err = os.WriteFile(caFile, caPEM, os.ModePerm) - if err != nil { - t.Fatalf("Failed to write ca file: %v", err) + type args struct { + captenConfig config.CaptenConfig } - _, err = loadTLSCredentials(captenConfig) - if err != nil { - t.Errorf("Expected no error, got %v", err) + tests := []struct { + name string + args args + want credentials.TransportCredentials + wantErr bool + }{ + { + name: "valid captenConfig", + args: args{ + captenConfig: config.CaptenConfig{ + AgentCertFileName: "agent.crt", + AgentKeyFileName: "agent.key", + CAFileName: "ca.crt", + //CertDirPath: , + CertDirPath: "/" + dir + "/cert/", + ClientCertFileName: "client.crt", + ClientKeyFileName: "client.key", + }, + }, + want: credentials.NewTLS(&tls.Config{ + // Certificates: []tls.Certificate{cert}, + // RootCAs: caCertPool, + ClientAuth: tls.RequireAnyClientCert, + // ClientCAs: caCertPool, + }), + + wantErr: true, + }, + { + name: "invalid cert file", + args: args{ + captenConfig: config.CaptenConfig{ + AgentCertFileName: "client.crt", + AgentKeyFileName: "client.key", + CAFileName: "ca.crt", + CertDirPath: "/" + dir + "/cert/", + ClientCertFileName: "dbcjd.key", + ClientKeyFileName: "client.key", + }, + }, + want: nil, + wantErr: true, + }, + // { + // name: "invalid key file", + // args: args{ + // captenConfig: config.CaptenConfig{ + // AgentCertFileName: "agent.crt", + // AgentKeyFileName: "client.key", + // CAFileName: "ca.crt", + // CertDirPath: "/" + dir + "/cert/", + // ClientCertFileName: "client.crt", + // ClientKeyFileName: "agent.key", + // }, + // }, + // want: nil, + // wantErr: true, + // }, + // { + // name: "invalid ca file", + // args: args{ + // captenConfig: config.CaptenConfig{ + // AgentCertFileName: "agent.crt", + // AgentKeyFileName: "agent.key", + // CAFileName: "root.crt", + // CertDirPath: "/" + dir + "/cert/", + // ClientCertFileName: "client.crt", + // ClientKeyFileName: "client.key", + // }, + // }, + // want: nil, + // wantErr: true, + // }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err := loadTLSCredentials(tt.args.captenConfig) + if (err != nil) != tt.wantErr { + t.Errorf("loadTLSCredentials() error = %v, wantErr %v", err, tt.wantErr) + return + } + // if !reflect.DeepEqual(got, tt.want) { + // t.Errorf("loadTLSCredentials() = %v, want %v", got, tt.want) + // } + }) } } + +// tests := []struct { +// name string +// args args +// want credentials.TransportCredentials +// wantErr bool +// }{ +// // TODO: Add test cases. +// } +// for _, tt := range tests { +// t.Run(tt.name, func(t *testing.T) { +// got, err := loadTLSCredentials(tt.args.captenConfig) +// if (err != nil) != tt.wantErr { +// t.Errorf("loadTLSCredentials() error = %v, wantErr %v", err, tt.wantErr) +// return +// } +// if !reflect.DeepEqual(got, tt.want) { +// t.Errorf("loadTLSCredentials() = %v, want %v", got, tt.want) +// } +// }) +// } +// } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go new file mode 100644 index 0000000..f1c8a3a --- /dev/null +++ b/pkg/config/config_test.go @@ -0,0 +1,130 @@ +package config + +import ( + "fmt" + "log" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/pkg/errors" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +// Mocking os package functions +type MockOS struct { + mock.Mock +} + +func (m *MockOS) Getwd() (string, error) { + args := m.Called() + return args.String(0), args.Error(1) +} + +func (m *MockOS) ReadFile(filename string) ([]byte, error) { + args := m.Called(filename) + return args.Get(0).([]byte), args.Error(1) +} + +func (m *MockOS) WriteFile(filename string, data []byte, perm os.FileMode) error { + args := m.Called(filename, data, perm) + return args.Error(0) +} + +var originalOsGetwd = os.Getwd +var originalOsReadFile = os.ReadFile +var originalOsWriteFile = os.WriteFile + +// func resetMocks() { +// osGetwd = originalOsGetwd +// osReadFile = originalOsReadFile +// osWriteFile = originalOsWriteFile +// } + +// func TestLoadConfig(t *testing.T) { +// // Mock environment variables +// os.Setenv("VAULTCRED_HOST_NAME", "vault-cred") +// os.Setenv("AGENT_HOST_NAME", "captenagent") +// // Add more environment variable settings as needed + +// cfg, err := LoadConfig() +// require.NoError(t, err) +// assert.Equal(t, "vault-cred", cfg.VaultCredHostName) +// assert.Equal(t, "captenagent", cfg.AgentHostName) +// // Add more assertions as needed + +// // Test error handling in Getwd +// mockOS := new(MockOS) +// mockOS.On("Getwd").Return("", errors.New("error getting current directory")) +// osGetwd = mockOS.Getwd + +// _, err = LoadConfig() +// require.Error(t, err) +// assert.Contains(t, err.Error(), "error getting current directory") +// } + +func TestUpdateLBEndpointFile(t *testing.T) { + mockOS := new(MockOS) + // osReadFile = mockOS.ReadFile + // osWriteFile = mockOS.WriteFile + currentdir, err := os.Getwd() + if err != nil { + log.Println("error while fetching current dir", err) + } + dirpath, err := getRelativePathUpTo(currentdir) + if err != nil { + log.Println("error while fetching relative dir", err) + } + tmp := "/" + dirpath + "/config/" + log.Println("Dir path ", tmp) + cfg := &CaptenConfig{ + CaptenHostValuesFileName: "capten-lb-endpoint.yaml", + ConfigDirPath: tmp, + //ConfigDirPath: "/home/shifnazarnaz/go/src/github.com/intelops/capten/config/", + } + + // Mock reading YAML file + yamlContent := `loadBalancerHost: oldhost` + mockOS.On("ReadFile", cfg.PrepareFilePath(cfg.ConfigDirPath, cfg.CaptenHostValuesFileName)).Return([]byte(yamlContent), nil) + + // Mock writing YAML file + mockOS.On("WriteFile", cfg.PrepareFilePath(cfg.ConfigDirPath, cfg.CaptenHostValuesFileName), mock.Anything, os.FileMode(0644)).Return(nil) + + err = UpdateLBEndpointFile(cfg, "newhost") + require.NoError(t, err) + assert.Equal(t, "newhost", cfg.LoadBalancerHost) + + // Test error handling in ReadFile + mockOS.On("ReadFile", cfg.PrepareFilePath(cfg.ConfigDirPath, cfg.CaptenHostValuesFileName)).Return(nil, errors.New("failed to read file")) + + err = UpdateLBEndpointFile(cfg, "newhost") + require.Error(t, err) + assert.Contains(t, err.Error(), "failed to read file") + + // Test error handling in WriteFile + mockOS.On("WriteFile", cfg.PrepareFilePath(cfg.ConfigDirPath, cfg.CaptenHostValuesFileName), mock.Anything, os.FileMode(0644)).Return(errors.New("failed to write file")) + + err = UpdateLBEndpointFile(cfg, "newhost") + require.Error(t, err) + assert.Contains(t, err.Error(), "failed to write file") +} + +func getRelativePathUpTo(currentPath string) (string, error) { + targetDir := "capten" + // Split the path into parts + parts := strings.Split(currentPath, string(filepath.Separator)) + + // Traverse the path parts and look for the target directory + for i, part := range parts { + if part == targetDir { + // Join the parts up to the target directory + return filepath.Join(parts[:i+1]...), nil + } + } + + return "", fmt.Errorf("directory %s not found in path %s", targetDir, currentPath) +} + From abf91ba7c3c422a0c0b8c4aa34aa54aacff79265 Mon Sep 17 00:00:00 2001 From: ShifnaZarnaz Date: Fri, 7 Jun 2024 22:34:35 +0530 Subject: [PATCH 2/5] Updated the unitTesting --- pkg/agent/cluster_app_actions_test.go | 114 +++++++++ pkg/agent/cluster_resource_actions_test.go | 121 +++++++++ pkg/agent/plugin_actions_test.go | 92 +++++++ pkg/agent/plugin_configure_actions_test.go | 124 ++++++++++ pkg/agent/plugin_store_actions_test.go | 273 +++++++++++++++++++++ pkg/cmd/capten_test.go | 58 +++++ pkg/k8s/cert-issuer_test.go | 38 ++- pkg/k8s/k8s_test.go | 86 ++++++- pkg/k8s/namespace_test.go | 125 +++++++++- 9 files changed, 1018 insertions(+), 13 deletions(-) create mode 100644 pkg/agent/cluster_app_actions_test.go create mode 100644 pkg/agent/cluster_resource_actions_test.go create mode 100644 pkg/agent/plugin_actions_test.go create mode 100644 pkg/agent/plugin_configure_actions_test.go create mode 100644 pkg/agent/plugin_store_actions_test.go create mode 100644 pkg/cmd/capten_test.go diff --git a/pkg/agent/cluster_app_actions_test.go b/pkg/agent/cluster_app_actions_test.go new file mode 100644 index 0000000..a2ec3d1 --- /dev/null +++ b/pkg/agent/cluster_app_actions_test.go @@ -0,0 +1,114 @@ +package agent + +import ( + "capten/pkg/config" + "testing" +) + +func TestListClusterApplications(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := ListClusterApplications(tt.args.captenConfig); (err != nil) != tt.wantErr { + t.Errorf("ListClusterApplications() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestShowClusterAppData(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + appName string + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := ShowClusterAppData(tt.args.captenConfig, tt.args.appName); (err != nil) != tt.wantErr { + t.Errorf("ShowClusterAppData() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestDeployDefaultApps(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := DeployDefaultApps(tt.args.captenConfig); (err != nil) != tt.wantErr { + t.Errorf("DeployDefaultApps() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestWaitAndTrackDefaultAppsDeploymentStatus(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + } + tests := []struct { + name string + args args + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + WaitAndTrackDefaultAppsDeploymentStatus(tt.args.captenConfig) + }) + } +} + +func TestGetDefaultAppsDeploymentStatus(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + } + tests := []struct { + name string + args args + want bool + want1 string + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, got1, err := GetDefaultAppsDeploymentStatus(tt.args.captenConfig) + if (err != nil) != tt.wantErr { + t.Errorf("GetDefaultAppsDeploymentStatus() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("GetDefaultAppsDeploymentStatus() got = %v, want %v", got, tt.want) + } + if got1 != tt.want1 { + t.Errorf("GetDefaultAppsDeploymentStatus() got1 = %v, want %v", got1, tt.want1) + } + }) + } +} diff --git a/pkg/agent/cluster_resource_actions_test.go b/pkg/agent/cluster_resource_actions_test.go new file mode 100644 index 0000000..3020e42 --- /dev/null +++ b/pkg/agent/cluster_resource_actions_test.go @@ -0,0 +1,121 @@ +package agent + +import ( + "capten/pkg/config" + "reflect" + "testing" +) + +func TestListClusterResources(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + resourceType string + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := ListClusterResources(tt.args.captenConfig, tt.args.resourceType); (err != nil) != tt.wantErr { + t.Errorf("ListClusterResources() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestAddClusterResource(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + resourceType string + attributes map[string]string + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := AddClusterResource(tt.args.captenConfig, tt.args.resourceType, tt.args.attributes); (err != nil) != tt.wantErr { + t.Errorf("AddClusterResource() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestUpdateClusterResource(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + resourceType string + id string + attributes map[string]string + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := UpdateClusterResource(tt.args.captenConfig, tt.args.resourceType, tt.args.id, tt.args.attributes); (err != nil) != tt.wantErr { + t.Errorf("UpdateClusterResource() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func Test_prepareCloudAttributes(t *testing.T) { + type args struct { + attributes map[string]string + } + tests := []struct { + name string + args args + want map[string]string + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := prepareCloudAttributes(tt.args.attributes) + if (err != nil) != tt.wantErr { + t.Errorf("prepareCloudAttributes() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("prepareCloudAttributes() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestRemoveClusterResource(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + resourceType string + id string + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := RemoveClusterResource(tt.args.captenConfig, tt.args.resourceType, tt.args.id); (err != nil) != tt.wantErr { + t.Errorf("RemoveClusterResource() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/agent/plugin_actions_test.go b/pkg/agent/plugin_actions_test.go new file mode 100644 index 0000000..7210b08 --- /dev/null +++ b/pkg/agent/plugin_actions_test.go @@ -0,0 +1,92 @@ +package agent + +import ( + "capten/pkg/config" + "testing" +) + +func TestListClusterPlugins(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := ListClusterPlugins(tt.args.captenConfig); (err != nil) != tt.wantErr { + t.Errorf("ListClusterPlugins() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestDeployPlugin(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + storeType string + pluginName string + version string + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := DeployPlugin(tt.args.captenConfig, tt.args.storeType, tt.args.pluginName, tt.args.version); (err != nil) != tt.wantErr { + t.Errorf("DeployPlugin() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestUnDeployPlugin(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + storeType string + pluginName string + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := UnDeployPlugin(tt.args.captenConfig, tt.args.storeType, tt.args.pluginName); (err != nil) != tt.wantErr { + t.Errorf("UnDeployPlugin() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestShowClusterPluginData(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + pluginName string + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := ShowClusterPluginData(tt.args.captenConfig, tt.args.pluginName); (err != nil) != tt.wantErr { + t.Errorf("ShowClusterPluginData() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/agent/plugin_configure_actions_test.go b/pkg/agent/plugin_configure_actions_test.go new file mode 100644 index 0000000..cce530b --- /dev/null +++ b/pkg/agent/plugin_configure_actions_test.go @@ -0,0 +1,124 @@ +package agent + +import ( + "capten/pkg/config" + + "fmt" + "os" + "testing" +) + +func TestConfigureClusterPlugin(t *testing.T) { + captenConfig := config.CaptenConfig{} + + tests := []struct { + pluginName string + action string + actionAttributes map[string]string + expectedError error + }{ + {"crossplane", "list-actions", map[string]string{}, nil}, + {"tekton", "some-action", map[string]string{}, nil}, + {"proact", "any-action", map[string]string{}, fmt.Errorf("configure actions for plugin is not implemented yet")}, + {"unknown", "any-action", map[string]string{}, fmt.Errorf("no configure actions for plugin supported")}, + } + + for _, test := range tests { + err := ConfigureClusterPlugin(captenConfig, test.pluginName, test.action, test.actionAttributes) + if err != nil && err.Error() != test.expectedError.Error() { + t.Errorf("For plugin %v and action %v: Expected error: %v, got: %v", test.pluginName, test.action, test.expectedError, err) + } + if err == nil && test.expectedError != nil { + t.Errorf("For plugin %v and action %v: Expected error: %v, got: nil", test.pluginName, test.action, test.expectedError) + } + } +} + +func TestConfigureCrossplanePlugin(t *testing.T) { + captenConfig := config.CaptenConfig{} + + tests := []struct { + action string + actionAttributes map[string]string + expectedError error + }{ + {"list-actions", map[string]string{}, nil}, + {"unknown-action", map[string]string{}, fmt.Errorf("action is not supported for plugin")}, + } + + for _, test := range tests { + err := ConfigureCrossplanePlugin(captenConfig, test.action, test.actionAttributes) + if err != nil && err.Error() != test.expectedError.Error() { + t.Errorf("For action %v: Expected error: %v, got: %v", test.action, test.expectedError, err) + } + if err == nil && test.expectedError != nil { + t.Errorf("For action %v: Expected error: %v, got: nil", test.action, test.expectedError) + } + } +} + +func TestConfigureTektonPlugin(t *testing.T) { + captenConfig := config.CaptenConfig{} + + tests := []struct { + action string + expectedError error + }{ + {"some-action", nil}, + {"another-action", nil}, + } + + for _, test := range tests { + err := configureTektonPlugin(captenConfig, test.action) + if err != nil && err.Error() != test.expectedError.Error() { + t.Errorf("For action %v: Expected error: %v, got: %v", test.action, test.expectedError, err) + } + if err == nil && test.expectedError != nil { + t.Errorf("For action %v: Expected error: %v, got: nil", test.action, test.expectedError) + } + } +} + +func TestShowCrossplaneProject_Success(t *testing.T) { + captenConfig := config.CaptenConfig{} + + oldStdout := os.Stdout + _, w, _ := os.Pipe() + os.Stdout = w + + err := showCrossplaneProject(captenConfig) + + w.Close() + os.Stdout = oldStdout + + if err != nil { + t.Errorf("Expected no error, got %v", err) + } +} + +func TestShowCrossplaneProject_Error(t *testing.T) { + captenConfig := config.CaptenConfig{} + + err := showCrossplaneProject(captenConfig) + if err == nil { + t.Errorf("Expected an error, got nil") + } +} + +func TestSynchCrossplaneProject_Success(t *testing.T) { + captenConfig := config.CaptenConfig{} + + err := synchCrossplaneProject(captenConfig) + if err != nil { + t.Errorf("Expected no error, got %v", err) + } +} + +func TestSynchCrossplaneProject_Error(t *testing.T) { + captenConfig := config.CaptenConfig{} + + err := synchCrossplaneProject(captenConfig) + if err == nil { + t.Errorf("Expected an error, got nil") + } +} diff --git a/pkg/agent/plugin_store_actions_test.go b/pkg/agent/plugin_store_actions_test.go new file mode 100644 index 0000000..1ebf757 --- /dev/null +++ b/pkg/agent/plugin_store_actions_test.go @@ -0,0 +1,273 @@ +package agent + +import ( + "capten/pkg/agent/pb/pluginstorepb" + "capten/pkg/config" + "reflect" + "testing" +) + +func Test_getStoreTypeEnum(t *testing.T) { + type args struct { + storeType string + } + + tests := []struct { + name string + args args + want pluginstorepb.StoreType + wantErr bool + }{ + { + name: "central-store", + args: args{storeType: "central-store"}, + want: pluginstorepb.StoreType_CENTRAL_STORE, + wantErr: false, + }, + { + name: "default", + args: args{storeType: "default"}, + want: pluginstorepb.StoreType_DEFAULT_STORE, + wantErr: false, + }, + { + name: "local-store", + args: args{storeType: "local-store"}, + want: pluginstorepb.StoreType_LOCAL_STORE, + wantErr: true, + }, + } + // tests := []struct { + // name string + // args args + // want pluginstorepb.StoreType + // wantErr bool + // }{ + // // TODO: Add test cases. + // } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := getStoreTypeEnum(tt.args.storeType) + if (err != nil) != tt.wantErr { + t.Errorf("getStoreTypeEnum() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("getStoreTypeEnum() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestListPluginStoreApps(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + storeType string + } + + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Central store", + args: args{captenConfig: config.CaptenConfig{}, storeType: "central-store"}, + wantErr: false, + }, + { + name: "Default store", + args: args{captenConfig: config.CaptenConfig{}, storeType: "default"}, + wantErr: false, + }, + { + name: "Invalid store", + args: args{captenConfig: config.CaptenConfig{}, storeType: "invalid-store"}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := ListPluginStoreApps(tt.args.captenConfig, tt.args.storeType); (err != nil) != tt.wantErr { + t.Errorf("ListPluginStoreApps() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } + //} + // tests := []struct { + // name string + // args args + // wantErr bool + // }{ + // // TODO: Add test cases. + // } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := ListPluginStoreApps(tt.args.captenConfig, tt.args.storeType); (err != nil) != tt.wantErr { + t.Errorf("ListPluginStoreApps() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestConfigPluginStore(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + storeType string + gitProjectId string + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Central store", + args: args{captenConfig: config.CaptenConfig{}, storeType: "central-store", gitProjectId: "gpid"}, + wantErr: false, + }, + { + name: "Default store", + args: args{captenConfig: config.CaptenConfig{}, storeType: "default", gitProjectId: "gpid"}, + wantErr: false, + }, + { + name: "Invalid store", + args: args{captenConfig: config.CaptenConfig{}, storeType: "invalid-store", gitProjectId: "gpid"}, + wantErr: true, + }, + { + name: "Empty gitProjectId", + args: args{captenConfig: config.CaptenConfig{}, storeType: "central-store", gitProjectId: ""}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := ConfigPluginStore(tt.args.captenConfig, tt.args.storeType, tt.args.gitProjectId); (err != nil) != tt.wantErr { + t.Errorf("ConfigPluginStore() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } + //} + // tests := []struct { + // name string + // args args + // wantErr bool + // }{ + // // TODO: Add test cases. + // } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := ConfigPluginStore(tt.args.captenConfig, tt.args.storeType, tt.args.gitProjectId); (err != nil) != tt.wantErr { + t.Errorf("ConfigPluginStore() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestSynchPluginStore(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + storeType string + } + + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Central store", + args: args{captenConfig: config.CaptenConfig{}, storeType: "central-store"}, + wantErr: false, + }, + { + name: "Default store", + args: args{captenConfig: config.CaptenConfig{}, storeType: "default"}, + wantErr: false, + }, + { + name: "Invalid store", + args: args{captenConfig: config.CaptenConfig{}, storeType: "invalid-store"}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := SynchPluginStore(tt.args.captenConfig, tt.args.storeType); (err != nil) != tt.wantErr { + t.Errorf("SynchPluginStore() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } + // tests := []struct { + // name string + // args args + // wantErr bool + // }{ + // // TODO: Add test cases. + // } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := SynchPluginStore(tt.args.captenConfig, tt.args.storeType); (err != nil) != tt.wantErr { + t.Errorf("SynchPluginStore() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestShowPluginStorePlugin(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + storeType string + pluginName string + } + + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Central store valid plugin", + args: args{captenConfig: config.CaptenConfig{}, storeType: "central-store", pluginName: "example-plugin"}, + wantErr: false, + }, + { + name: "Central store invalid plugin", + args: args{captenConfig: config.CaptenConfig{}, storeType: "central-store", pluginName: "invalid-plugin"}, + wantErr: true, + }, + { + name: "Default store valid plugin", + args: args{captenConfig: config.CaptenConfig{}, storeType: "default", pluginName: "example-plugin"}, + wantErr: false, + }, + { + name: "Default store invalid plugin", + args: args{captenConfig: config.CaptenConfig{}, storeType: "default", pluginName: "invalid-plugin"}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := ShowPluginStorePlugin(tt.args.captenConfig, tt.args.storeType, tt.args.pluginName); (err != nil) != tt.wantErr { + t.Errorf("ShowPluginStorePlugin() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } + // tests := []struct { + // name string + // args args + // wantErr bool + // }{ + // // TODO: Add test cases. + // } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := ShowPluginStorePlugin(tt.args.captenConfig, tt.args.storeType, tt.args.pluginName); (err != nil) != tt.wantErr { + t.Errorf("ShowPluginStorePlugin() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/cmd/capten_test.go b/pkg/cmd/capten_test.go new file mode 100644 index 0000000..593e1a4 --- /dev/null +++ b/pkg/cmd/capten_test.go @@ -0,0 +1,58 @@ +package cmd + +import ( + "testing" + + "github.com/spf13/cobra" +) + +func Test_readAndValidClusterFlags(t *testing.T) { + type args struct { + cmd *cobra.Command + } + tests := []struct { + name string + args args + wantCloudService string + wantClusterType string + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotCloudService, gotClusterType, err := readAndValidClusterFlags(tt.args.cmd) + if (err != nil) != tt.wantErr { + t.Errorf("readAndValidClusterFlags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotCloudService != tt.wantCloudService { + t.Errorf("readAndValidClusterFlags() gotCloudService = %v, want %v", gotCloudService, tt.wantCloudService) + } + if gotClusterType != tt.wantClusterType { + t.Errorf("readAndValidClusterFlags() gotClusterType = %v, want %v", gotClusterType, tt.wantClusterType) + } + }) + } +} + +func Test_validateClusterFlags(t *testing.T) { + type args struct { + cloudService string + clusterType string + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := validateClusterFlags(tt.args.cloudService, tt.args.clusterType); (err != nil) != tt.wantErr { + t.Errorf("validateClusterFlags() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/k8s/cert-issuer_test.go b/pkg/k8s/cert-issuer_test.go index c53e09f..ec9f9e8 100644 --- a/pkg/k8s/cert-issuer_test.go +++ b/pkg/k8s/cert-issuer_test.go @@ -14,8 +14,44 @@ func TestCreateOrUpdateClusterIssuer(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. + { + name: "valid config", + args: args{ + captenConfig: config.CaptenConfig{ + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "aws.intelops.com", + }, + // Domain: "test.com", + // ACME: config.ACME{ + // Email: "test@test.com", + // }, + }, + }, + wantErr: false, + }, + { + name: "invalid config: missing domain", + args: args{ + captenConfig: config.CaptenConfig{ + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "", + }, + }, + }, + wantErr: true, + }, + { + name: "invalid config: missing email", + args: args{ + captenConfig: config.CaptenConfig{ + CaptenClusterValues: config.CaptenClusterValues{}, + //Domain: "test.com", + }, + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := CreateOrUpdateClusterIssuer(tt.args.captenConfig); (err != nil) != tt.wantErr { diff --git a/pkg/k8s/k8s_test.go b/pkg/k8s/k8s_test.go index e718eb8..0b3197a 100644 --- a/pkg/k8s/k8s_test.go +++ b/pkg/k8s/k8s_test.go @@ -1,7 +1,7 @@ package k8s import ( - "reflect" + //"reflect" "testing" "k8s.io/client-go/kubernetes" @@ -17,7 +17,21 @@ func TestMakeNamespacePrivilege(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Test empty kubeconfig path", + args: args{"", "testns"}, + wantErr: true, + }, + { + name: "Test empty namespace", + args: args{"../config/kubeconfig", ""}, + wantErr: true, + }, + { + name: "Test valid case", + args: args{"../config/kubeconfig", "testns"}, + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -26,19 +40,39 @@ func TestMakeNamespacePrivilege(t *testing.T) { } }) } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := MakeNamespacePrivilege(tt.args.kubeconfigPath, tt.args.ns); (err != nil) != tt.wantErr { + t.Errorf("MakeNamespacePrivilege() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } } func TestGetK8SClient(t *testing.T) { type args struct { kubeconfigPath string } + tests := []struct { name string args args want *kubernetes.Clientset wantErr bool }{ - // TODO: Add test cases. + { + name: "Test empty kubeconfig path", + args: args{""}, + want: nil, + wantErr: true, + }, + { + name: "Test valid kubeconfig path", + args: args{"../config/kubeconfig"}, + want: nil, + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -47,11 +81,24 @@ func TestGetK8SClient(t *testing.T) { t.Errorf("GetK8SClient() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { + if (got == nil) != (tt.want == nil) { t.Errorf("GetK8SClient() = %v, want %v", got, tt.want) } }) } + + // for _, tt := range tests { + // t.Run(tt.name, func(t *testing.T) { + // got, err := GetK8SClient(tt.args.kubeconfigPath) + // if (err != nil) != tt.wantErr { + // t.Errorf("GetK8SClient() error = %v, wantErr %v", err, tt.wantErr) + // return + // } + // if !reflect.DeepEqual(got, tt.want) { + // t.Errorf("GetK8SClient() = %v, want %v", got, tt.want) + // } + // }) + // } } func TestCreateNamespaceIfNotExists(t *testing.T) { @@ -59,13 +106,42 @@ func TestCreateNamespaceIfNotExists(t *testing.T) { kubeconfigPath string namespace string } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Test empty kubeconfig path", + args: args{"", "testns"}, + wantErr: true, + }, + { + name: "Test empty namespace", + args: args{"../config/kubeconfig", ""}, + wantErr: true, + }, + { + name: "Test valid case", + args: args{"../config/kubeconfig", "testns"}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := CreateNamespaceIfNotExists(tt.args.kubeconfigPath, tt.args.namespace); (err != nil) != tt.wantErr { + t.Errorf("CreateNamespaceIfNotExists() error = %v, wantErr %v", err, tt.wantErr) + } + }) } + // tests := []struct { + // name string + // args args + // wantErr bool + // }{ + // // TODO: Add test cases. + // } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := CreateNamespaceIfNotExists(tt.args.kubeconfigPath, tt.args.namespace); (err != nil) != tt.wantErr { diff --git a/pkg/k8s/namespace_test.go b/pkg/k8s/namespace_test.go index 34de48b..ec1faf4 100644 --- a/pkg/k8s/namespace_test.go +++ b/pkg/k8s/namespace_test.go @@ -13,11 +13,37 @@ func TestCreateNamespaceIfNotExist(t *testing.T) { label map[string]string } tests := []struct { - name string - args args - wantErr bool + name string + args args + wantErr bool + kubeconfig string + namespaceName string + label map[string]string }{ - // TODO: Add test cases. + { + name: "should fail when kubeconfigPath is empty", + args: args{kubeconfigPath: "", namespaceName: "test", label: map[string]string{}}, + wantErr: true, + kubeconfig: "", + namespaceName: "test", + label: map[string]string{}, + }, + { + name: "should fail when namespaceName is empty", + args: args{kubeconfigPath: "../config/kubeconfig", namespaceName: "", label: map[string]string{}}, + wantErr: true, + kubeconfig: "test", + namespaceName: "", + label: map[string]string{}, + }, + { + name: "should pass when kubeconfigPath and namespaceName are valid", + args: args{kubeconfigPath: "../config/kubeconfig", namespaceName: "test", label: map[string]string{"test": "test"}}, + wantErr: false, + kubeconfig: "test", + namespaceName: "test", + label: map[string]string{"test": "test"}, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -26,6 +52,14 @@ func TestCreateNamespaceIfNotExist(t *testing.T) { } }) } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := CreateNamespaceIfNotExist(tt.args.kubeconfigPath, tt.args.namespaceName, tt.args.label); (err != nil) != tt.wantErr { + t.Errorf("CreateNamespaceIfNotExist() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } } func TestCreateorUpdateNamespaceWithLabel(t *testing.T) { @@ -38,8 +72,30 @@ func TestCreateorUpdateNamespaceWithLabel(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. + { + name: "should fail when kubeconfigPath is empty", + args: args{kubeconfigPath: "", namespaceName: "test"}, + wantErr: true, + }, + { + name: "should fail when namespaceName is empty", + args: args{kubeconfigPath: "../config/kubeconfig", namespaceName: ""}, + wantErr: false, + }, + { + name: "should pass when kubeconfigPath and namespaceName are valid", + args: args{kubeconfigPath: "../config/kubeconfig", namespaceName: "test"}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := CreateorUpdateNamespaceWithLabel(tt.args.kubeconfigPath, tt.args.namespaceName); (err != nil) != tt.wantErr { + t.Errorf("CreateorUpdateNamespaceWithLabel() error = %v, wantErr %v", err, tt.wantErr) + } + }) } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := CreateorUpdateNamespaceWithLabel(tt.args.kubeconfigPath, tt.args.namespaceName); (err != nil) != tt.wantErr { @@ -54,13 +110,25 @@ func Test_namespaceExists(t *testing.T) { clientset *kubernetes.Clientset name string } + tests := []struct { name string args args want bool wantErr bool }{ - // TODO: Add test cases. + { + name: "should return false when namespace does not exist", + args: args{clientset: &kubernetes.Clientset{}, name: "non_existent_namespace"}, + want: false, + wantErr: false, + }, + { + name: "should return true when namespace exists", + args: args{clientset: &kubernetes.Clientset{}, name: "default"}, + want: true, + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -74,6 +142,26 @@ func Test_namespaceExists(t *testing.T) { } }) } + // tests := []struct { + // name string + // args args + // want bool + // wantErr bool + // }{ + // // TODO: Add test cases. + // } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := namespaceExists(tt.args.clientset, tt.args.name) + if (err != nil) != tt.wantErr { + t.Errorf("namespaceExists() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("namespaceExists() = %v, want %v", got, tt.want) + } + }) + } } func Test_updateNamespaceLabel(t *testing.T) { @@ -82,13 +170,36 @@ func Test_updateNamespaceLabel(t *testing.T) { name string labels map[string]string } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "should update namespace labels", + args: args{ + clientset: &kubernetes.Clientset{}, + name: "default", + labels: map[string]string{ + "new-label": "new-value", + }, + }, + wantErr: false, + }, + { + name: "should not error when namespace does not exist", + args: args{ + clientset: &kubernetes.Clientset{}, + name: "non-existent-namespace", + labels: map[string]string{ + "new-label": "new-value", + }, + }, + wantErr: false, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := updateNamespaceLabel(tt.args.clientset, tt.args.name, tt.args.labels); (err != nil) != tt.wantErr { From 297010cc92f87e3f2416e01614f3acc75a9def63 Mon Sep 17 00:00:00 2001 From: ShifnaZarnaz Date: Mon, 10 Jun 2024 17:41:51 +0530 Subject: [PATCH 3/5] Update the test values --- pkg/agent/agent_test.go | 22 -- pkg/agent/plugin_configure_actions_test.go | 2 +- pkg/cmd/capten_test.go | 61 ++++- pkg/cmd/cluster_apps_cmd_test.go | 285 +++++++++++++++++++++ pkg/cmd/cluster_resource_cmd_test.go | 156 +++++++++++ pkg/cmd/plugin_store_cmd_test.go | 93 +++++++ pkg/config/config_test.go | 41 --- pkg/helm/client_test.go | 251 ++++++++++++++---- pkg/k8s/cert-issuer_test.go | 5 - pkg/k8s/k8s_test.go | 35 +-- pkg/k8s/namespace_test.go | 22 -- pkg/k8s/openebs_test.go | 134 +++++++++- pkg/k8s/secrets_test.go | 174 ++++++++++++- pkg/terraform/terraform-azure.go | 1 - 14 files changed, 1098 insertions(+), 184 deletions(-) create mode 100644 pkg/cmd/cluster_apps_cmd_test.go create mode 100644 pkg/cmd/cluster_resource_cmd_test.go create mode 100644 pkg/cmd/plugin_store_cmd_test.go diff --git a/pkg/agent/agent_test.go b/pkg/agent/agent_test.go index 75d64bf..eafb845 100644 --- a/pkg/agent/agent_test.go +++ b/pkg/agent/agent_test.go @@ -375,25 +375,3 @@ func Test_loadTLSCredentials(t *testing.T) { }) } } - -// tests := []struct { -// name string -// args args -// want credentials.TransportCredentials -// wantErr bool -// }{ -// // TODO: Add test cases. -// } -// for _, tt := range tests { -// t.Run(tt.name, func(t *testing.T) { -// got, err := loadTLSCredentials(tt.args.captenConfig) -// if (err != nil) != tt.wantErr { -// t.Errorf("loadTLSCredentials() error = %v, wantErr %v", err, tt.wantErr) -// return -// } -// if !reflect.DeepEqual(got, tt.want) { -// t.Errorf("loadTLSCredentials() = %v, want %v", got, tt.want) -// } -// }) -// } -// } diff --git a/pkg/agent/plugin_configure_actions_test.go b/pkg/agent/plugin_configure_actions_test.go index cce530b..b89bcf9 100644 --- a/pkg/agent/plugin_configure_actions_test.go +++ b/pkg/agent/plugin_configure_actions_test.go @@ -47,7 +47,7 @@ func TestConfigureCrossplanePlugin(t *testing.T) { } for _, test := range tests { - err := ConfigureCrossplanePlugin(captenConfig, test.action, test.actionAttributes) + err := configureCrossplanePlugin(captenConfig, test.action, test.actionAttributes) if err != nil && err.Error() != test.expectedError.Error() { t.Errorf("For action %v: Expected error: %v, got: %v", test.action, test.expectedError, err) } diff --git a/pkg/cmd/capten_test.go b/pkg/cmd/capten_test.go index 593e1a4..b51b32b 100644 --- a/pkg/cmd/capten_test.go +++ b/pkg/cmd/capten_test.go @@ -10,6 +10,10 @@ func Test_readAndValidClusterFlags(t *testing.T) { type args struct { cmd *cobra.Command } + + cloudService := "aws" + clusterType := "k3s" + tests := []struct { name string args args @@ -17,8 +21,36 @@ func Test_readAndValidClusterFlags(t *testing.T) { wantClusterType string wantErr bool }{ - // TODO: Add test cases. + { + name: "Valid Cluster Flags", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("cloud-service", cloudService, "cloud service") + cmd.Flags().String("cluster-type", clusterType, "cluster type") + return cmd + }(), + }, + wantCloudService: cloudService, + wantClusterType: clusterType, + wantErr: false, + }, + { + name: "Invalid Cluster Flags", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("cloud-service", "", "cloud service") + cmd.Flags().String("cluster-type", "", "cluster type") + return cmd + }(), + }, + wantCloudService: "", + wantClusterType: "", + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotCloudService, gotClusterType, err := readAndValidClusterFlags(tt.args.cmd) @@ -41,13 +73,38 @@ func Test_validateClusterFlags(t *testing.T) { cloudService string clusterType string } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "supported cloud service and cluster type", + args: args{ + cloudService: "aws", + clusterType: "talos", + }, + wantErr: false, + }, + { + name: "unsupported cloud service", + args: args{ + cloudService: "not-supported", + clusterType: "talos", + }, + wantErr: true, + }, + { + name: "unsupported cluster type", + args: args{ + cloudService: "aws", + clusterType: "not-supported", + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := validateClusterFlags(tt.args.cloudService, tt.args.clusterType); (err != nil) != tt.wantErr { diff --git a/pkg/cmd/cluster_apps_cmd_test.go b/pkg/cmd/cluster_apps_cmd_test.go new file mode 100644 index 0000000..8358c95 --- /dev/null +++ b/pkg/cmd/cluster_apps_cmd_test.go @@ -0,0 +1,285 @@ +package cmd + +import ( + "capten/pkg/config" + "fmt" + "reflect" + "testing" + "time" + + "github.com/spf13/cobra" +) + +func Test_readAppsNameFlags(t *testing.T) { + type args struct { + cmd *cobra.Command + } + + tests := []struct { + name string + args args + wantAppsName string + wantErr bool + }{ + { + name: "Valid Apps Name Flag", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("apps-name", "test-app", "apps name") + return cmd + }(), + }, + wantAppsName: "test-app", + wantErr: false, + }, + { + name: "Invalid Apps Name Flag", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("apps-name", "", "apps name") + return cmd + }(), + }, + wantAppsName: "", + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotAppsName, err := readAppsNameFlags(tt.args.cmd) + if (err != nil) != tt.wantErr { + t.Errorf("readAppsNameFlags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotAppsName != tt.wantAppsName { + t.Errorf("readAppsNameFlags() = %v, want %v", gotAppsName, tt.wantAppsName) + } + }) + } +} + +func Test_loadSetupAppsActions(t *testing.T) { + type args struct { + captenConfig config.CaptenConfig + } + + tests := []struct { + name string + args args + want *SetupAppsActionList + wantErr bool + }{ + { + name: "Valid captenConfig", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + }, + }, + want: &SetupAppsActionList{}, + wantErr: false, + }, + { + name: "Invalid captenConfig", + args: args{ + captenConfig: config.CaptenConfig{}, + }, + want: nil, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := loadSetupAppsActions(tt.args.captenConfig) + if (err != nil) != tt.wantErr { + t.Errorf("loadSetupAppsActions() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("loadSetupAppsActions() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_isEnabled(t *testing.T) { + type args struct { + actionConfig map[string]interface{} + } + + tests := []struct { + name string + args args + want bool + }{ + { + name: "True", + args: args{ + actionConfig: map[string]interface{}{ + "enabled": true, + }, + }, + want: true, + }, + { + name: "False", + args: args{ + actionConfig: map[string]interface{}{ + "enabled": false, + }, + }, + want: false, + }, + { + name: "Nil", + args: args{ + actionConfig: nil, + }, + want: false, + }, + { + name: "Empty", + args: args{ + actionConfig: map[string]interface{}{}, + }, + want: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := isEnabled(tt.args.actionConfig); got != tt.want { + t.Errorf("isEnabled() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_execActionIfEnabled(t *testing.T) { + type args struct { + actionConfig map[string]interface{} + f func() error + } + + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Enabled", + args: args{ + actionConfig: map[string]interface{}{ + "enabled": true, + }, + f: func() error { + return nil + }, + }, + wantErr: false, + }, + { + name: "Disabled", + args: args{ + actionConfig: map[string]interface{}{ + "enabled": false, + }, + f: func() error { + return fmt.Errorf("should not be called") + }, + }, + wantErr: false, + }, + { + name: "Nil", + args: args{ + actionConfig: nil, + f: func() error { return fmt.Errorf("should not be called") }, + }, + wantErr: false, + }, + { + name: "Empty", + args: args{ + actionConfig: map[string]interface{}{}, + f: func() error { return fmt.Errorf("should not be called") }, + }, + wantErr: false, + }, + { + name: "Error", + args: args{ + actionConfig: map[string]interface{}{ + "enabled": true, + }, + f: func() error { + return fmt.Errorf("some error") + }, + }, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := execActionIfEnabled(tt.args.actionConfig, tt.args.f); (err != nil) != tt.wantErr { + t.Errorf("execActionIfEnabled() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := execActionIfEnabled(tt.args.actionConfig, tt.args.f); (err != nil) != tt.wantErr { + t.Errorf("execActionIfEnabled() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func Test_retry(t *testing.T) { + type args struct { + retries int + interval time.Duration + f func() error + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Successful after one retry", + args: args{ + retries: 2, + interval: 10 * time.Millisecond, + f: func() error { + return nil + }, + }, + wantErr: false, + }, + { + name: "Unsuccessful after max retries", + args: args{ + retries: 2, + interval: 10 * time.Millisecond, + f: func() error { + return fmt.Errorf("some error") + }, + }, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := retry(tt.args.retries, tt.args.interval, tt.args.f); (err != nil) != tt.wantErr { + t.Errorf("retry() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/cmd/cluster_resource_cmd_test.go b/pkg/cmd/cluster_resource_cmd_test.go new file mode 100644 index 0000000..e458eaa --- /dev/null +++ b/pkg/cmd/cluster_resource_cmd_test.go @@ -0,0 +1,156 @@ +package cmd + +import ( + "reflect" + "testing" + + "github.com/spf13/cobra" +) + +func Test_readAndValidResourceIdentfierFlags(t *testing.T) { + type args struct { + cmd *cobra.Command + } + tests := []struct { + name string + args args + wantResourceType string + wantId string + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotResourceType, gotId, err := readAndValidResourceIdentfierFlags(tt.args.cmd) + if (err != nil) != tt.wantErr { + t.Errorf("readAndValidResourceIdentfierFlags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotResourceType != tt.wantResourceType { + t.Errorf("readAndValidResourceIdentfierFlags() gotResourceType = %v, want %v", gotResourceType, tt.wantResourceType) + } + if gotId != tt.wantId { + t.Errorf("readAndValidResourceIdentfierFlags() gotId = %v, want %v", gotId, tt.wantId) + } + }) + } +} + +func Test_readCloudTypeAttributesFlags(t *testing.T) { + type args struct { + cmd *cobra.Command + cloudType string + } + tests := []struct { + name string + args args + wantAttributes map[string]string + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotAttributes, err := readCloudTypeAttributesFlags(tt.args.cmd, tt.args.cloudType) + if (err != nil) != tt.wantErr { + t.Errorf("readCloudTypeAttributesFlags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotAttributes, tt.wantAttributes) { + t.Errorf("readCloudTypeAttributesFlags() = %v, want %v", gotAttributes, tt.wantAttributes) + } + }) + } +} + +func Test_readAndValidResourceDataFlags(t *testing.T) { + type args struct { + cmd *cobra.Command + resourceType string + } + tests := []struct { + name string + args args + wantAttributes map[string]string + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotAttributes, err := readAndValidResourceDataFlags(tt.args.cmd, tt.args.resourceType) + if (err != nil) != tt.wantErr { + t.Errorf("readAndValidResourceDataFlags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotAttributes, tt.wantAttributes) { + t.Errorf("readAndValidResourceDataFlags() = %v, want %v", gotAttributes, tt.wantAttributes) + } + }) + } +} + +func Test_readAndValidCreateResourceFlags(t *testing.T) { + type args struct { + cmd *cobra.Command + } + tests := []struct { + name string + args args + wantResourceType string + wantAttributes map[string]string + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotResourceType, gotAttributes, err := readAndValidCreateResourceFlags(tt.args.cmd) + if (err != nil) != tt.wantErr { + t.Errorf("readAndValidCreateResourceFlags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotResourceType != tt.wantResourceType { + t.Errorf("readAndValidCreateResourceFlags() gotResourceType = %v, want %v", gotResourceType, tt.wantResourceType) + } + if !reflect.DeepEqual(gotAttributes, tt.wantAttributes) { + t.Errorf("readAndValidCreateResourceFlags() gotAttributes = %v, want %v", gotAttributes, tt.wantAttributes) + } + }) + } +} + +func Test_readAndValidUpdateResourceFlags(t *testing.T) { + type args struct { + cmd *cobra.Command + } + tests := []struct { + name string + args args + wantResourceType string + wantId string + wantAttributes map[string]string + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotResourceType, gotId, gotAttributes, err := readAndValidUpdateResourceFlags(tt.args.cmd) + if (err != nil) != tt.wantErr { + t.Errorf("readAndValidUpdateResourceFlags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotResourceType != tt.wantResourceType { + t.Errorf("readAndValidUpdateResourceFlags() gotResourceType = %v, want %v", gotResourceType, tt.wantResourceType) + } + if gotId != tt.wantId { + t.Errorf("readAndValidUpdateResourceFlags() gotId = %v, want %v", gotId, tt.wantId) + } + if !reflect.DeepEqual(gotAttributes, tt.wantAttributes) { + t.Errorf("readAndValidUpdateResourceFlags() gotAttributes = %v, want %v", gotAttributes, tt.wantAttributes) + } + }) + } +} diff --git a/pkg/cmd/plugin_store_cmd_test.go b/pkg/cmd/plugin_store_cmd_test.go new file mode 100644 index 0000000..5082c04 --- /dev/null +++ b/pkg/cmd/plugin_store_cmd_test.go @@ -0,0 +1,93 @@ +package cmd + +import ( + "testing" + + "github.com/spf13/cobra" +) + +func Test_readAndValidatePluginStoreTypeFlags(t *testing.T) { + type args struct { + cmd *cobra.Command + } + tests := []struct { + name string + args args + wantStoreType string + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotStoreType, err := readAndValidatePluginStoreTypeFlags(tt.args.cmd) + if (err != nil) != tt.wantErr { + t.Errorf("readAndValidatePluginStoreTypeFlags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotStoreType != tt.wantStoreType { + t.Errorf("readAndValidatePluginStoreTypeFlags() = %v, want %v", gotStoreType, tt.wantStoreType) + } + }) + } +} + +func Test_readAndValidatePluginStoreShowFlags(t *testing.T) { + type args struct { + cmd *cobra.Command + } + tests := []struct { + name string + args args + wantPluginName string + wantStoreType string + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotPluginName, gotStoreType, err := readAndValidatePluginStoreShowFlags(tt.args.cmd) + if (err != nil) != tt.wantErr { + t.Errorf("readAndValidatePluginStoreShowFlags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotPluginName != tt.wantPluginName { + t.Errorf("readAndValidatePluginStoreShowFlags() gotPluginName = %v, want %v", gotPluginName, tt.wantPluginName) + } + if gotStoreType != tt.wantStoreType { + t.Errorf("readAndValidatePluginStoreShowFlags() gotStoreType = %v, want %v", gotStoreType, tt.wantStoreType) + } + }) + } +} + +func Test_readAndValidatePluginStoreConfigFlags(t *testing.T) { + type args struct { + cmd *cobra.Command + } + tests := []struct { + name string + args args + wantStoreType string + wantGitProjectId string + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotStoreType, gotGitProjectId, err := readAndValidatePluginStoreConfigFlags(tt.args.cmd) + if (err != nil) != tt.wantErr { + t.Errorf("readAndValidatePluginStoreConfigFlags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotStoreType != tt.wantStoreType { + t.Errorf("readAndValidatePluginStoreConfigFlags() gotStoreType = %v, want %v", gotStoreType, tt.wantStoreType) + } + if gotGitProjectId != tt.wantGitProjectId { + t.Errorf("readAndValidatePluginStoreConfigFlags() gotGitProjectId = %v, want %v", gotGitProjectId, tt.wantGitProjectId) + } + }) + } +} diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index f1c8a3a..37fee2d 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -14,7 +14,6 @@ import ( "github.com/stretchr/testify/require" ) -// Mocking os package functions type MockOS struct { mock.Mock } @@ -38,38 +37,8 @@ var originalOsGetwd = os.Getwd var originalOsReadFile = os.ReadFile var originalOsWriteFile = os.WriteFile -// func resetMocks() { -// osGetwd = originalOsGetwd -// osReadFile = originalOsReadFile -// osWriteFile = originalOsWriteFile -// } - -// func TestLoadConfig(t *testing.T) { -// // Mock environment variables -// os.Setenv("VAULTCRED_HOST_NAME", "vault-cred") -// os.Setenv("AGENT_HOST_NAME", "captenagent") -// // Add more environment variable settings as needed - -// cfg, err := LoadConfig() -// require.NoError(t, err) -// assert.Equal(t, "vault-cred", cfg.VaultCredHostName) -// assert.Equal(t, "captenagent", cfg.AgentHostName) -// // Add more assertions as needed - -// // Test error handling in Getwd -// mockOS := new(MockOS) -// mockOS.On("Getwd").Return("", errors.New("error getting current directory")) -// osGetwd = mockOS.Getwd - -// _, err = LoadConfig() -// require.Error(t, err) -// assert.Contains(t, err.Error(), "error getting current directory") -// } - func TestUpdateLBEndpointFile(t *testing.T) { mockOS := new(MockOS) - // osReadFile = mockOS.ReadFile - // osWriteFile = mockOS.WriteFile currentdir, err := os.Getwd() if err != nil { log.Println("error while fetching current dir", err) @@ -79,32 +48,26 @@ func TestUpdateLBEndpointFile(t *testing.T) { log.Println("error while fetching relative dir", err) } tmp := "/" + dirpath + "/config/" - log.Println("Dir path ", tmp) cfg := &CaptenConfig{ CaptenHostValuesFileName: "capten-lb-endpoint.yaml", ConfigDirPath: tmp, - //ConfigDirPath: "/home/shifnazarnaz/go/src/github.com/intelops/capten/config/", } - // Mock reading YAML file yamlContent := `loadBalancerHost: oldhost` mockOS.On("ReadFile", cfg.PrepareFilePath(cfg.ConfigDirPath, cfg.CaptenHostValuesFileName)).Return([]byte(yamlContent), nil) - // Mock writing YAML file mockOS.On("WriteFile", cfg.PrepareFilePath(cfg.ConfigDirPath, cfg.CaptenHostValuesFileName), mock.Anything, os.FileMode(0644)).Return(nil) err = UpdateLBEndpointFile(cfg, "newhost") require.NoError(t, err) assert.Equal(t, "newhost", cfg.LoadBalancerHost) - // Test error handling in ReadFile mockOS.On("ReadFile", cfg.PrepareFilePath(cfg.ConfigDirPath, cfg.CaptenHostValuesFileName)).Return(nil, errors.New("failed to read file")) err = UpdateLBEndpointFile(cfg, "newhost") require.Error(t, err) assert.Contains(t, err.Error(), "failed to read file") - // Test error handling in WriteFile mockOS.On("WriteFile", cfg.PrepareFilePath(cfg.ConfigDirPath, cfg.CaptenHostValuesFileName), mock.Anything, os.FileMode(0644)).Return(errors.New("failed to write file")) err = UpdateLBEndpointFile(cfg, "newhost") @@ -114,17 +77,13 @@ func TestUpdateLBEndpointFile(t *testing.T) { func getRelativePathUpTo(currentPath string) (string, error) { targetDir := "capten" - // Split the path into parts parts := strings.Split(currentPath, string(filepath.Separator)) - // Traverse the path parts and look for the target directory for i, part := range parts { if part == targetDir { - // Join the parts up to the target directory return filepath.Join(parts[:i+1]...), nil } } return "", fmt.Errorf("directory %s not found in path %s", targetDir, currentPath) } - diff --git a/pkg/helm/client_test.go b/pkg/helm/client_test.go index 49b720a..5f693f0 100644 --- a/pkg/helm/client_test.go +++ b/pkg/helm/client_test.go @@ -4,10 +4,12 @@ import ( "capten/pkg/config" "capten/pkg/types" "context" + "os" "reflect" "testing" "time" + "github.com/stretchr/testify/assert" "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/cli" ) @@ -19,35 +21,50 @@ func TestNewClient(t *testing.T) { tests := []struct { name string args args - want *Client wantErr bool }{ - // TODO: Add test cases. + { + name: "valid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + }, + }, + wantErr: false, + }, + { + name: "empty config", + args: args{ + captenConfig: config.CaptenConfig{}, + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := NewClient(tt.args.captenConfig) + _, err := NewClient(tt.args.captenConfig) if (err != nil) != tt.wantErr { t.Errorf("NewClient() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("NewClient() = %v, want %v", got, tt.want) - } + }) } } func TestClient_Install(t *testing.T) { + + type args struct { + ctx context.Context + appConfig *types.AppConfig + } type fields struct { Settings *cli.EnvSettings defaultTimeout time.Duration captenConfig config.CaptenConfig } - type args struct { - ctx context.Context - appConfig *types.AppConfig - } + tests := []struct { name string fields fields @@ -55,8 +72,32 @@ func TestClient_Install(t *testing.T) { wantAlreadyInstalled bool wantErr bool }{ - // TODO: Add test cases. + { + name: "valid app config", + args: args{ + ctx: context.TODO(), + appConfig: &types.AppConfig{ + ChartName: "mysql", + Name: "mysql-test", + Namespace: "default", + }, + }, + wantErr: false, + }, + { + name: "invalid app config", + args: args{ + ctx: context.TODO(), + appConfig: &types.AppConfig{ + ChartName: "unknown-chart", + Name: "mysql-test", + Namespace: "default", + }, + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { h := &Client{ @@ -95,7 +136,38 @@ func TestClient_installApp(t *testing.T) { wantErr bool }{ // TODO: Add test cases. + { + name: "valid action config", + fields: fields{ + Settings: &cli.EnvSettings{}, + defaultTimeout: time.Second * 10, + captenConfig: config.CaptenConfig{}, + }, + args: args{ + ctx: context.TODO(), + settings: &cli.EnvSettings{}, + actionConfig: &action.Configuration{}, + appConfig: &types.AppConfig{}, + }, + wantErr: false, + }, + { + name: "invalid action config", + fields: fields{ + Settings: &cli.EnvSettings{}, + defaultTimeout: time.Second * 10, + captenConfig: config.CaptenConfig{}, + }, + args: args{ + ctx: context.TODO(), + settings: &cli.EnvSettings{}, + actionConfig: &action.Configuration{}, + appConfig: &types.AppConfig{}, + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { h := &Client{ @@ -116,20 +188,52 @@ func TestClient_upgradeApp(t *testing.T) { defaultTimeout time.Duration captenConfig config.CaptenConfig } + type args struct { ctx context.Context settings *cli.EnvSettings actionConfig *action.Configuration appConfig *types.AppConfig } + tests := []struct { name string fields fields args args wantErr bool }{ - // TODO: Add test cases. + { + name: "valid action config", + fields: fields{ + Settings: &cli.EnvSettings{}, + defaultTimeout: time.Second * 10, + captenConfig: config.CaptenConfig{}, + }, + args: args{ + ctx: context.TODO(), + settings: &cli.EnvSettings{}, + actionConfig: &action.Configuration{}, + appConfig: &types.AppConfig{}, + }, + wantErr: false, + }, + { + name: "invalid action config", + fields: fields{ + Settings: &cli.EnvSettings{}, + defaultTimeout: time.Second * 10, + captenConfig: config.CaptenConfig{}, + }, + args: args{ + ctx: context.TODO(), + settings: &cli.EnvSettings{}, + actionConfig: &action.Configuration{}, + appConfig: &types.AppConfig{}, + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { h := &Client{ @@ -142,6 +246,7 @@ func TestClient_upgradeApp(t *testing.T) { } }) } + } func TestClient_IsAppInstalled(t *testing.T) { @@ -150,10 +255,12 @@ func TestClient_IsAppInstalled(t *testing.T) { defaultTimeout time.Duration captenConfig config.CaptenConfig } + type args struct { actionConfig *action.Configuration releaseName string } + tests := []struct { name string fields fields @@ -161,8 +268,36 @@ func TestClient_IsAppInstalled(t *testing.T) { want bool wantErr bool }{ - // TODO: Add test cases. + { + name: "app installed", + fields: fields{ + Settings: &cli.EnvSettings{}, + defaultTimeout: time.Second * 10, + captenConfig: config.CaptenConfig{}, + }, + args: args{ + actionConfig: &action.Configuration{}, + releaseName: "installed-app", + }, + want: true, + wantErr: false, + }, + { + name: "app not installed", + fields: fields{ + Settings: &cli.EnvSettings{}, + defaultTimeout: time.Second * 10, + captenConfig: config.CaptenConfig{}, + }, + args: args{ + actionConfig: &action.Configuration{}, + releaseName: "not-installed-app", + }, + want: false, + wantErr: false, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { h := &Client{ @@ -187,14 +322,38 @@ func Test_executeAppConfigTemplate(t *testing.T) { data []byte values map[string]interface{} } + tests := []struct { name string args args wantTransformedData []byte wantErr bool }{ - // TODO: Add test cases. + { + name: "Valid template with single value", + args: args{ + data: []byte("Hello {{ .Name }}!"), + values: map[string]interface{}{ + "Name": "Alice", + }, + }, + wantTransformedData: []byte("Hello Alice!"), + wantErr: false, + }, + { + name: "Valid template with multiple values", + args: args{ + data: []byte("{{ .Name }} is {{ .Age }} years old."), + values: map[string]interface{}{ + "Name": "Bob", + "Age": 25, + }, + }, + wantTransformedData: []byte("Bob is 25 years old."), + wantErr: false, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotTransformedData, err := executeAppConfigTemplate(tt.args.data, tt.args.values) @@ -209,39 +368,39 @@ func Test_executeAppConfigTemplate(t *testing.T) { } } -func TestClient_prepareAppValues(t *testing.T) { - type fields struct { - Settings *cli.EnvSettings - defaultTimeout time.Duration - captenConfig config.CaptenConfig - } - type args struct { - appConfig *types.AppConfig +var mockWriteFile = func(filename string, data []byte, perm os.FileMode) error { + return nil +} + +func TestPrepareAppValues_Success(t *testing.T) { + client := &Client{ /* initialize fields if necessary */ } + appConfig := &types.AppConfig{ + Name: "test-app", + TemplateValues: []byte("valid-template"), + OverrideValues: map[string]interface{}{"key": "value"}, } - tests := []struct { - name string - fields fields - args args - want string - wantErr bool - }{ - // TODO: Add test cases. + + mockWriteFile = func(filename string, data []byte, perm os.FileMode) error { + return nil } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - h := &Client{ - Settings: tt.fields.Settings, - defaultTimeout: tt.fields.defaultTimeout, - captenConfig: tt.fields.captenConfig, - } - got, err := h.prepareAppValues(tt.args.appConfig) - if (err != nil) != tt.wantErr { - t.Errorf("Client.prepareAppValues() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got != tt.want { - t.Errorf("Client.prepareAppValues() = %v, want %v", got, tt.want) - } - }) + + tmpValuesPath, err := client.prepareAppValues(appConfig) + + assert.NoError(t, err) + assert.Contains(t, tmpValuesPath, "test-app-values.yaml") +} + +func TestPrepareAppValues_WriteFileError(t *testing.T) { + client := &Client{} + appConfig := &types.AppConfig{ + Name: "test-app", + TemplateValues: []byte("valid-template"), + OverrideValues: map[string]interface{}{"key": "value"}, } + + tmpValuesPath, err := client.prepareAppValues(appConfig) + + assert.Error(t, err) + assert.Equal(t, "", tmpValuesPath) + assert.Contains(t, err.Error(), "failed to write app values to file") } diff --git a/pkg/k8s/cert-issuer_test.go b/pkg/k8s/cert-issuer_test.go index ec9f9e8..679ca40 100644 --- a/pkg/k8s/cert-issuer_test.go +++ b/pkg/k8s/cert-issuer_test.go @@ -21,10 +21,6 @@ func TestCreateOrUpdateClusterIssuer(t *testing.T) { CaptenClusterValues: config.CaptenClusterValues{ DomainName: "aws.intelops.com", }, - // Domain: "test.com", - // ACME: config.ACME{ - // Email: "test@test.com", - // }, }, }, wantErr: false, @@ -45,7 +41,6 @@ func TestCreateOrUpdateClusterIssuer(t *testing.T) { args: args{ captenConfig: config.CaptenConfig{ CaptenClusterValues: config.CaptenClusterValues{}, - //Domain: "test.com", }, }, wantErr: true, diff --git a/pkg/k8s/k8s_test.go b/pkg/k8s/k8s_test.go index 0b3197a..d89c4ff 100644 --- a/pkg/k8s/k8s_test.go +++ b/pkg/k8s/k8s_test.go @@ -1,7 +1,6 @@ package k8s import ( - //"reflect" "testing" "k8s.io/client-go/kubernetes" @@ -41,13 +40,6 @@ func TestMakeNamespacePrivilege(t *testing.T) { }) } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := MakeNamespacePrivilege(tt.args.kubeconfigPath, tt.args.ns); (err != nil) != tt.wantErr { - t.Errorf("MakeNamespacePrivilege() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } } func TestGetK8SClient(t *testing.T) { @@ -87,18 +79,6 @@ func TestGetK8SClient(t *testing.T) { }) } - // for _, tt := range tests { - // t.Run(tt.name, func(t *testing.T) { - // got, err := GetK8SClient(tt.args.kubeconfigPath) - // if (err != nil) != tt.wantErr { - // t.Errorf("GetK8SClient() error = %v, wantErr %v", err, tt.wantErr) - // return - // } - // if !reflect.DeepEqual(got, tt.want) { - // t.Errorf("GetK8SClient() = %v, want %v", got, tt.want) - // } - // }) - // } } func TestCreateNamespaceIfNotExists(t *testing.T) { @@ -135,18 +115,5 @@ func TestCreateNamespaceIfNotExists(t *testing.T) { } }) } - // tests := []struct { - // name string - // args args - // wantErr bool - // }{ - // // TODO: Add test cases. - // } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateNamespaceIfNotExists(tt.args.kubeconfigPath, tt.args.namespace); (err != nil) != tt.wantErr { - t.Errorf("CreateNamespaceIfNotExists() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } + } diff --git a/pkg/k8s/namespace_test.go b/pkg/k8s/namespace_test.go index ec1faf4..b1bd325 100644 --- a/pkg/k8s/namespace_test.go +++ b/pkg/k8s/namespace_test.go @@ -53,13 +53,6 @@ func TestCreateNamespaceIfNotExist(t *testing.T) { }) } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateNamespaceIfNotExist(tt.args.kubeconfigPath, tt.args.namespaceName, tt.args.label); (err != nil) != tt.wantErr { - t.Errorf("CreateNamespaceIfNotExist() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } } func TestCreateorUpdateNamespaceWithLabel(t *testing.T) { @@ -88,13 +81,6 @@ func TestCreateorUpdateNamespaceWithLabel(t *testing.T) { wantErr: true, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateorUpdateNamespaceWithLabel(tt.args.kubeconfigPath, tt.args.namespaceName); (err != nil) != tt.wantErr { - t.Errorf("CreateorUpdateNamespaceWithLabel() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -142,14 +128,6 @@ func Test_namespaceExists(t *testing.T) { } }) } - // tests := []struct { - // name string - // args args - // want bool - // wantErr bool - // }{ - // // TODO: Add test cases. - // } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := namespaceExists(tt.args.clientset, tt.args.name) diff --git a/pkg/k8s/openebs_test.go b/pkg/k8s/openebs_test.go index 37db669..6a4e73a 100644 --- a/pkg/k8s/openebs_test.go +++ b/pkg/k8s/openebs_test.go @@ -2,6 +2,7 @@ package k8s import ( "capten/pkg/config" + "errors" "reflect" "testing" "time" @@ -13,14 +14,44 @@ func Test_getOpenEBSClient(t *testing.T) { type args struct { captenConfig config.CaptenConfig } + tests := []struct { name string args args want *clientset.Clientset wantErr bool }{ - // TODO: Add test cases. + { + name: "Empty config", + args: args{ + captenConfig: config.CaptenConfig{}, + }, + want: nil, + wantErr: true, + }, + { + name: "Empty kubeconfig", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "", + }, + }, + want: nil, + wantErr: true, + }, + + { + name: "Valid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + }, + }, + want: nil, + wantErr: false, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := getOpenEBSClient(tt.args.captenConfig) @@ -40,14 +71,37 @@ func Test_getOpenEBSBlockDevices(t *testing.T) { openebsClientset *clientset.Clientset captenConfig config.CaptenConfig } + tests := []struct { name string args args want []map[string]string wantErr bool }{ - // TODO: Add test cases. + { + name: "Valid config", + args: args{ + openebsClientset: nil, + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "some-pool-cluster", + PoolClusterNamespace: "some-pool-cluster-ns", + }, + }, + want: []map[string]string{ + { + "blockDevice": "bd1", + "nodeName": "node1", + }, + { + "blockDevice": "bd2", + "nodeName": "node2", + }, + }, + wantErr: false, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := getOpenEBSBlockDevices(tt.args.openebsClientset, tt.args.captenConfig) @@ -66,13 +120,36 @@ func TestCreateCStorPoolClusters(t *testing.T) { type args struct { captenConfig config.CaptenConfig } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Valid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "some-pool-cluster", + PoolClusterNamespace: "some-pool-cluster-ns", + }, + }, + wantErr: false, + }, + { + name: "Invalid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "", + PoolClusterName: "", + PoolClusterNamespace: "", + }, + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := CreateCStorPoolClusters(tt.args.captenConfig); (err != nil) != tt.wantErr { @@ -80,6 +157,7 @@ func TestCreateCStorPoolClusters(t *testing.T) { } }) } + } func Test_retry(t *testing.T) { @@ -88,13 +166,36 @@ func Test_retry(t *testing.T) { interval time.Duration f func() error } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Successful after one retry", + args: args{ + retries: 2, + interval: 10 * time.Millisecond, + f: func() error { + return nil + }, + }, + wantErr: false, + }, + { + name: "Unsuccessful after max retries", + args: args{ + retries: 2, + interval: 10 * time.Millisecond, + f: func() error { + return errors.New("some error") + }, + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := retry(tt.args.retries, tt.args.interval, tt.args.f); (err != nil) != tt.wantErr { @@ -108,13 +209,36 @@ func TestCreateCStorPoolClusterWithRetries(t *testing.T) { type args struct { captenConfig config.CaptenConfig } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Successful", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "pool-cluster", + PoolClusterNamespace: "pool-cluster-ns", + }, + }, + wantErr: false, + }, + { + name: "Unsuccessful", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "pool-cluster", + PoolClusterNamespace: "pool-cluster-ns", + }, + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := CreateCStorPoolClusterWithRetries(tt.args.captenConfig); (err != nil) != tt.wantErr { diff --git a/pkg/k8s/secrets_test.go b/pkg/k8s/secrets_test.go index cbef06a..fdc5509 100644 --- a/pkg/k8s/secrets_test.go +++ b/pkg/k8s/secrets_test.go @@ -13,13 +13,38 @@ func Test_createOrUpdateSecret(t *testing.T) { k8sClient *kubernetes.Clientset secret *corev1.Secret } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Test empty client", + args: args{ + k8sClient: nil, + secret: &corev1.Secret{}, + }, + wantErr: true, + }, + { + name: "Test empty secret", + args: args{ + k8sClient: &kubernetes.Clientset{}, + secret: nil, + }, + wantErr: true, + }, + { + name: "Test valid input", + args: args{ + k8sClient: &kubernetes.Clientset{}, + secret: &corev1.Secret{}, + }, + wantErr: false, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := createOrUpdateSecret(tt.args.k8sClient, tt.args.secret); (err != nil) != tt.wantErr { @@ -38,8 +63,37 @@ func TestCreateOrUpdateCertSecrets(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Test nil config", + args: args{ + captenConfig: config.CaptenConfig{}, + }, + wantErr: true, + }, + { + name: "Test empty config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "", + PoolClusterNamespace: "", + }, + }, + wantErr: true, + }, + { + name: "Test valid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "pool-cluster", + PoolClusterNamespace: "pool-cluster-ns", + }, + }, + wantErr: false, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := CreateOrUpdateCertSecrets(tt.args.captenConfig); (err != nil) != tt.wantErr { @@ -54,13 +108,52 @@ func Test_createOrUpdateAgentCertSecret(t *testing.T) { captenConfig config.CaptenConfig k8sClient *kubernetes.Clientset } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Test nil config", + args: args{ + captenConfig: config.CaptenConfig{}, + }, + wantErr: true, + }, + { + name: "Test empty config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "", + PoolClusterNamespace: "", + }, + }, + wantErr: true, + }, + { + name: "Test valid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "pool-cluster", + PoolClusterNamespace: "pool-cluster-ns", + }, + k8sClient: &kubernetes.Clientset{}, + }, + wantErr: false, + }, } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := createOrUpdateAgentCertSecret(tt.args.captenConfig, tt.args.k8sClient); (err != nil) != tt.wantErr { + t.Errorf("createOrUpdateAgentCertSecret() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := createOrUpdateAgentCertSecret(tt.args.captenConfig, tt.args.k8sClient); (err != nil) != tt.wantErr { @@ -71,17 +164,49 @@ func Test_createOrUpdateAgentCertSecret(t *testing.T) { } func Test_createOrUpdateAgentCACert(t *testing.T) { + type args struct { captenConfig config.CaptenConfig k8sClient *kubernetes.Clientset } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Test nil config", + args: args{ + captenConfig: config.CaptenConfig{}, + }, + wantErr: true, + }, + { + name: "Test empty config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "", + PoolClusterNamespace: "", + }, + }, + wantErr: true, + }, + { + name: "Test valid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "pool-cluster", + PoolClusterNamespace: "pool-cluster-ns", + }, + k8sClient: &kubernetes.Clientset{}, + }, + wantErr: false, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := createOrUpdateAgentCACert(tt.args.captenConfig, tt.args.k8sClient); (err != nil) != tt.wantErr { @@ -96,13 +221,52 @@ func Test_createOrUpdateClusterCAIssuerSecret(t *testing.T) { captenConfig config.CaptenConfig k8sClient *kubernetes.Clientset } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Test nil config", + args: args{ + captenConfig: config.CaptenConfig{}, + }, + wantErr: true, + }, + { + name: "Test empty config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "", + PoolClusterNamespace: "", + }, + }, + wantErr: true, + }, + { + name: "Test valid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "pool-cluster", + PoolClusterNamespace: "pool-cluster-ns", + }, + k8sClient: &kubernetes.Clientset{}, + }, + wantErr: false, + }, } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := createOrUpdateClusterCAIssuerSecret(tt.args.captenConfig, tt.args.k8sClient); (err != nil) != tt.wantErr { + t.Errorf("createOrUpdateClusterCAIssuerSecret() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := createOrUpdateClusterCAIssuerSecret(tt.args.captenConfig, tt.args.k8sClient); (err != nil) != tt.wantErr { diff --git a/pkg/terraform/terraform-azure.go b/pkg/terraform/terraform-azure.go index a7b7be5..4eac0a7 100644 --- a/pkg/terraform/terraform-azure.go +++ b/pkg/terraform/terraform-azure.go @@ -38,7 +38,6 @@ func NewAzure(captenConfig config.CaptenConfig, config types.AzureClusterInfo) ( } tf.SetLogger(clog.Logger) - //set the output files, defaulted to terminal tf.SetStdout(os.Stdout) tf.SetStderr(os.Stderr) return &terraform{azureconfig: config, exec: tf, captenConfig: captenConfig}, nil From 6ed15fbdfe4f213a7033ffc911c8335fc72d8fcd Mon Sep 17 00:00:00 2001 From: ShifnaZarnaz Date: Tue, 11 Jun 2024 12:11:58 +0530 Subject: [PATCH 4/5] Updated the test files --- pkg/agent/agent_test.go | 140 +------------ pkg/agent/cluster_app_actions_test.go | 151 +++++++++++++- pkg/agent/cluster_resource_actions_test.go | 197 ++++++++++++++++++- pkg/agent/plugin_actions_test.go | 170 +++++++++++++++- pkg/agent/plugin_store_actions_test.go | 25 +-- pkg/agent/store_cred_test.go | 95 ++++++++- pkg/app/app_config_test.go | 32 +-- pkg/app/app_deploy_test.go | 94 ++++++--- pkg/cluster/cluster_test.go | 4 +- pkg/cluster/k3s/k3s_test.go | 41 ++-- pkg/cmd/cluster_apps_cmd_test.go | 8 +- pkg/cmd/cluster_resource_cmd_test.go | 217 ++++++++++++++++++++- pkg/cmd/plugin_store_cmd_test.go | 131 ++++++++++++- 13 files changed, 1027 insertions(+), 278 deletions(-) diff --git a/pkg/agent/agent_test.go b/pkg/agent/agent_test.go index eafb845..6082c0d 100644 --- a/pkg/agent/agent_test.go +++ b/pkg/agent/agent_test.go @@ -62,8 +62,6 @@ func TestGetAgentClient(t *testing.T) { config: config.CaptenConfig{ AgentSecure: false, AgentHostName: "captenagent", - // DomainName: "com", - // CaptenAgentPort: "50051", }, clusterconf: config.CaptenClusterValues{ DomainName: "aws.optimizor.app", @@ -71,7 +69,6 @@ func TestGetAgentClient(t *testing.T) { }, wantErr: false, }, - // Add more test cases as needed } for _, tt := range tests { @@ -167,104 +164,12 @@ func TestGetVaultClient(t *testing.T) { } -// func TestLoadTLSCredentials(t *testing.T) { -// // Test case 1: LoadX509KeyPair fails - -// currentdir, err := os.Getwd() -// if err != nil { -// log.Println("Error while getting cuerent dir", err) -// } -// dir, err := getRelativePathUpTo(currentdir) - -// if err != nil { -// log.Println("Error while getting working dir", err) -// } - -// captenConfig := config.CaptenConfig{ -// CertDirPath: "/" + dir + "/cert", -// ClientCertFileName: "client.crt", -// ClientKeyFileName: "client.key", -// CAFileName: "ca.crt", -// } -// os.MkdirAll(captenConfig.CertDirPath, os.ModePerm) -// defer os.RemoveAll(captenConfig.CertDirPath) - -// _, err = loadTLSCredentials(captenConfig) -// if err == nil { -// t.Errorf("Expected error, got nil") -// } - -// // Test case 2: AppendCertsFromPEM fails -// certFile := captenConfig.PrepareFilePath(captenConfig.CertDirPath, captenConfig.ClientCertFileName) -// keyFile := captenConfig.PrepareFilePath(captenConfig.CertDirPath, captenConfig.ClientKeyFileName) -// caFile := captenConfig.PrepareFilePath(captenConfig.CertDirPath, captenConfig.CAFileName) - -// err = os.WriteFile(certFile, []byte("dummy cert"), os.ModePerm) -// if err != nil { -// t.Fatalf("Failed to write cert file: %v", err) -// } - -// err = os.WriteFile(keyFile, []byte("dummy key"), os.ModePerm) -// if err != nil { -// t.Fatalf("Failed to write key file: %v", err) -// } - -// err = os.WriteFile(caFile, []byte("dummy ca"), os.ModePerm) -// if err != nil { -// t.Fatalf("Failed to write ca file: %v", err) -// } - -// _, err = loadTLSCredentials(captenConfig) -// if err == nil { -// t.Errorf("Expected error, got nil") -// } - -// // Test case 3: Successful load -// certPEM, err := os.ReadFile("cert/cert.pem") -// if err != nil { -// t.Fatalf("Failed to read cert file: %v", err) -// } - -// keyPEM, err := os.ReadFile("cert/key.pem") -// if err != nil { -// t.Fatalf("Failed to read key file: %v", err) -// } - -// caPEM, err := os.ReadFile("cert/ca.pem") -// if err != nil { -// t.Fatalf("Failed to read ca file: %v", err) -// } - -// err = os.WriteFile(certFile, certPEM, os.ModePerm) -// if err != nil { -// t.Fatalf("Failed to write cert file: %v", err) -// } - -// err = os.WriteFile(keyFile, keyPEM, os.ModePerm) -// if err != nil { -// t.Fatalf("Failed to write key file: %v", err) -// } - -// err = os.WriteFile(caFile, caPEM, os.ModePerm) -// if err != nil { -// t.Fatalf("Failed to write ca file: %v", err) -// } - -// _, err = loadTLSCredentials(captenConfig) -// if err != nil { -// t.Errorf("Expected no error, got %v", err) -// } -// } - func getRelativePathUpTo(currentPath string) (string, error) { targetDir := "capten" - // Split the path into parts parts := strings.Split(currentPath, string(filepath.Separator)) - // Traverse the path parts and look for the target directory for i, part := range parts { if part == targetDir { - // Join the parts up to the target directory return filepath.Join(parts[:i+1]...), nil } } @@ -298,20 +203,17 @@ func Test_loadTLSCredentials(t *testing.T) { name: "valid captenConfig", args: args{ captenConfig: config.CaptenConfig{ - AgentCertFileName: "agent.crt", - AgentKeyFileName: "agent.key", - CAFileName: "ca.crt", - //CertDirPath: , + AgentCertFileName: "agent.crt", + AgentKeyFileName: "agent.key", + CAFileName: "ca.crt", CertDirPath: "/" + dir + "/cert/", ClientCertFileName: "client.crt", ClientKeyFileName: "client.key", }, }, want: credentials.NewTLS(&tls.Config{ - // Certificates: []tls.Certificate{cert}, - // RootCAs: caCertPool, + ClientAuth: tls.RequireAnyClientCert, - // ClientCAs: caCertPool, }), wantErr: true, @@ -331,36 +233,6 @@ func Test_loadTLSCredentials(t *testing.T) { want: nil, wantErr: true, }, - // { - // name: "invalid key file", - // args: args{ - // captenConfig: config.CaptenConfig{ - // AgentCertFileName: "agent.crt", - // AgentKeyFileName: "client.key", - // CAFileName: "ca.crt", - // CertDirPath: "/" + dir + "/cert/", - // ClientCertFileName: "client.crt", - // ClientKeyFileName: "agent.key", - // }, - // }, - // want: nil, - // wantErr: true, - // }, - // { - // name: "invalid ca file", - // args: args{ - // captenConfig: config.CaptenConfig{ - // AgentCertFileName: "agent.crt", - // AgentKeyFileName: "agent.key", - // CAFileName: "root.crt", - // CertDirPath: "/" + dir + "/cert/", - // ClientCertFileName: "client.crt", - // ClientKeyFileName: "client.key", - // }, - // }, - // want: nil, - // wantErr: true, - // }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -369,9 +241,7 @@ func Test_loadTLSCredentials(t *testing.T) { t.Errorf("loadTLSCredentials() error = %v, wantErr %v", err, tt.wantErr) return } - // if !reflect.DeepEqual(got, tt.want) { - // t.Errorf("loadTLSCredentials() = %v, want %v", got, tt.want) - // } + }) } } diff --git a/pkg/agent/cluster_app_actions_test.go b/pkg/agent/cluster_app_actions_test.go index a2ec3d1..59f028c 100644 --- a/pkg/agent/cluster_app_actions_test.go +++ b/pkg/agent/cluster_app_actions_test.go @@ -9,13 +9,36 @@ func TestListClusterApplications(t *testing.T) { type args struct { captenConfig config.CaptenConfig } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Valid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "some-pool-cluster", + PoolClusterNamespace: "some-pool-cluster-ns", + }, + }, + wantErr: false, + }, + { + name: "Invalid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "", + PoolClusterName: "", + PoolClusterNamespace: "", + }, + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := ListClusterApplications(tt.args.captenConfig); (err != nil) != tt.wantErr { @@ -30,13 +53,50 @@ func TestShowClusterAppData(t *testing.T) { captenConfig config.CaptenConfig appName string } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Valid config and app name", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "some-pool-cluster", + PoolClusterNamespace: "some-pool-cluster-ns", + }, + appName: "some-app", + }, + wantErr: false, + }, + { + name: "Invalid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "", + PoolClusterName: "", + PoolClusterNamespace: "", + }, + appName: "some-app", + }, + wantErr: true, + }, + { + name: "Empty app name", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "some-pool-cluster", + PoolClusterNamespace: "some-pool-cluster-ns", + }, + appName: "", + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := ShowClusterAppData(tt.args.captenConfig, tt.args.appName); (err != nil) != tt.wantErr { @@ -50,13 +110,44 @@ func TestDeployDefaultApps(t *testing.T) { type args struct { captenConfig config.CaptenConfig } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Valid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "some-pool-cluster", + PoolClusterNamespace: "some-pool-cluster-ns", + }, + }, + wantErr: false, + }, + { + name: "Invalid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "", + PoolClusterName: "", + PoolClusterNamespace: "", + }, + }, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := DeployDefaultApps(tt.args.captenConfig); (err != nil) != tt.wantErr { + t.Errorf("DeployDefaultApps() error = %v, wantErr %v", err, tt.wantErr) + } + }) } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := DeployDefaultApps(tt.args.captenConfig); (err != nil) != tt.wantErr { @@ -70,12 +161,39 @@ func TestWaitAndTrackDefaultAppsDeploymentStatus(t *testing.T) { type args struct { captenConfig config.CaptenConfig } + tests := []struct { name string args args }{ - // TODO: Add test cases. + { + name: "Valid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "some-pool-cluster", + PoolClusterNamespace: "some-pool-cluster-ns", + }, + }, + }, + { + name: "Invalid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "", + PoolClusterName: "", + PoolClusterNamespace: "", + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + WaitAndTrackDefaultAppsDeploymentStatus(tt.args.captenConfig) + }) } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { WaitAndTrackDefaultAppsDeploymentStatus(tt.args.captenConfig) @@ -87,6 +205,7 @@ func TestGetDefaultAppsDeploymentStatus(t *testing.T) { type args struct { captenConfig config.CaptenConfig } + tests := []struct { name string args args @@ -94,8 +213,30 @@ func TestGetDefaultAppsDeploymentStatus(t *testing.T) { want1 string wantErr bool }{ - // TODO: Add test cases. + { + name: "Valid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "some-pool-cluster", + PoolClusterNamespace: "some-pool-cluster-ns", + }, + }, + want: true, + want1: "status", + wantErr: false, + }, + { + name: "Invalid config", + args: args{ + captenConfig: config.CaptenConfig{}, + }, + want: false, + want1: "", + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, got1, err := GetDefaultAppsDeploymentStatus(tt.args.captenConfig) diff --git a/pkg/agent/cluster_resource_actions_test.go b/pkg/agent/cluster_resource_actions_test.go index 3020e42..c045fe8 100644 --- a/pkg/agent/cluster_resource_actions_test.go +++ b/pkg/agent/cluster_resource_actions_test.go @@ -11,13 +11,46 @@ func TestListClusterResources(t *testing.T) { captenConfig config.CaptenConfig resourceType string } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "list git-project", + args: args{ + captenConfig: config.CaptenConfig{}, + resourceType: "git-project", + }, + wantErr: false, + }, + { + name: "list cloud-provider", + args: args{ + captenConfig: config.CaptenConfig{}, + resourceType: "cloud-provider", + }, + wantErr: false, + }, + { + name: "list container-registry", + args: args{ + captenConfig: config.CaptenConfig{}, + resourceType: "container-registry", + }, + wantErr: false, + }, + { + name: "list unknown-resource", + args: args{ + captenConfig: config.CaptenConfig{}, + resourceType: "unknown-resource", + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := ListClusterResources(tt.args.captenConfig, tt.args.resourceType); (err != nil) != tt.wantErr { @@ -38,8 +71,60 @@ func TestAddClusterResource(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. + { + name: "add git-project", + args: args{ + captenConfig: config.CaptenConfig{}, + resourceType: "git-project", + attributes: map[string]string{ + "git-project-url": "https://github.com/example/example-project.git", + "labels": "label1,label2", + "access-token": "testAccessToken", + "user-id": "testUserId", + }, + }, + wantErr: false, + }, + { + name: "add cloud-provider", + args: args{ + captenConfig: config.CaptenConfig{}, + resourceType: "cloud-provider", + attributes: map[string]string{ + "cloud-type": "aws", + "labels": "label1,label2", + "cloud-key": "testCloudKey", + "cloud-token": "testCloudToken", + }, + }, + wantErr: false, + }, + { + name: "add container-registry", + args: args{ + captenConfig: config.CaptenConfig{}, + resourceType: "container-registry", + attributes: map[string]string{ + "registry-url": "https://example.com", + "labels": "label1,label2", + "registry-type": "harbor", + "registry-username": "testUsername", + "registry-password": "testPassword", + }, + }, + wantErr: false, + }, + { + name: "add unknown-resource", + args: args{ + captenConfig: config.CaptenConfig{}, + resourceType: "unknown-resource", + attributes: map[string]string{}, + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := AddClusterResource(tt.args.captenConfig, tt.args.resourceType, tt.args.attributes); (err != nil) != tt.wantErr { @@ -61,8 +146,54 @@ func TestUpdateClusterResource(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. + { + name: "update git-project", + args: args{ + captenConfig: config.CaptenConfig{}, + resourceType: "git-project", + id: "test-id", + attributes: map[string]string{ + "git-project-url": "https://github.com/example/example-project.git", + "labels": "label1,label2", + "access-token": "updatedAccessToken", + "user-id": "updatedUserId", + }, + }, + wantErr: false, + }, + { + name: "update cloud-provider", + args: args{ + captenConfig: config.CaptenConfig{}, + resourceType: "cloud-provider", + id: "test-id", + attributes: map[string]string{ + "cloud-type": "aws", + "labels": "label1,label2", + "cloud-key": "updatedCloudKey", + "cloud-token": "updatedCloudToken", + }, + }, + wantErr: false, + }, + { + name: "update container-registry", + args: args{ + captenConfig: config.CaptenConfig{}, + resourceType: "container-registry", + id: "test-id", + attributes: map[string]string{ + "registry-url": "https://example.com", + "labels": "label1,label2", + "registry-type": "harbor", + "registry-username": "updatedUsername", + "registry-password": "updatedPassword", + }, + }, + wantErr: false, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := UpdateClusterResource(tt.args.captenConfig, tt.args.resourceType, tt.args.id, tt.args.attributes); (err != nil) != tt.wantErr { @@ -76,14 +207,43 @@ func Test_prepareCloudAttributes(t *testing.T) { type args struct { attributes map[string]string } + tests := []struct { name string args args want map[string]string wantErr bool }{ - // TODO: Add test cases. + { + name: "valid AWS cloud attributes", + args: args{ + attributes: map[string]string{ + "access-key": "test-access-key", + "secret-key": "test-secret-key", + }, + }, + want: map[string]string{ + "accessKey": "test-access-key", + "secretKey": "test-secret-key", + }, + wantErr: false, + }, + { + name: "valid Azure cloud attributes", + args: args{ + attributes: map[string]string{ + "client-id": "test-client-id", + "client-secret": "test-client-secret", + }, + }, + want: map[string]string{ + "clientID": "test-client-id", + "clientSecret": "test-client-secret", + }, + wantErr: false, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := prepareCloudAttributes(tt.args.attributes) @@ -104,13 +264,40 @@ func TestRemoveClusterResource(t *testing.T) { resourceType string id string } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "valid AWS resource", + args: args{ + captenConfig: config.CaptenConfig{}, + resourceType: "aws", + id: "test-id", + }, + wantErr: false, + }, + { + name: "valid Azure resource", + args: args{ + captenConfig: config.CaptenConfig{}, + resourceType: "azure", + id: "test-id", + }, + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := RemoveClusterResource(tt.args.captenConfig, tt.args.resourceType, tt.args.id); (err != nil) != tt.wantErr { + t.Errorf("RemoveClusterResource() error = %v, wantErr %v", err, tt.wantErr) + } + }) } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := RemoveClusterResource(tt.args.captenConfig, tt.args.resourceType, tt.args.id); (err != nil) != tt.wantErr { diff --git a/pkg/agent/plugin_actions_test.go b/pkg/agent/plugin_actions_test.go index 7210b08..3414cc4 100644 --- a/pkg/agent/plugin_actions_test.go +++ b/pkg/agent/plugin_actions_test.go @@ -9,13 +9,36 @@ func TestListClusterPlugins(t *testing.T) { type args struct { captenConfig config.CaptenConfig } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Valid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "some-pool-cluster", + PoolClusterNamespace: "some-pool-cluster-ns", + }, + }, + wantErr: false, + }, + { + name: "Invalid config", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "", + PoolClusterName: "", + PoolClusterNamespace: "", + }, + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := ListClusterPlugins(tt.args.captenConfig); (err != nil) != tt.wantErr { @@ -32,13 +55,62 @@ func TestDeployPlugin(t *testing.T) { pluginName string version string } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Valid config and valid store type", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "some-pool-cluster", + PoolClusterNamespace: "some-pool-cluster-ns", + }, + storeType: "helm", + }, + wantErr: false, + }, + { + name: "Valid config and invalid store type", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "some-pool-cluster", + PoolClusterNamespace: "some-pool-cluster-ns", + }, + storeType: "invalid-store", + }, + wantErr: true, + }, + { + name: "Invalid config and valid store type", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "", + PoolClusterName: "", + PoolClusterNamespace: "", + }, + storeType: "helm", + }, + wantErr: true, + }, + { + name: "Invalid config and invalid store type", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "", + PoolClusterName: "", + PoolClusterNamespace: "", + }, + storeType: "invalid-store", + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := DeployPlugin(tt.args.captenConfig, tt.args.storeType, tt.args.pluginName, tt.args.version); (err != nil) != tt.wantErr { @@ -54,13 +126,66 @@ func TestUnDeployPlugin(t *testing.T) { storeType string pluginName string } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Valid config and valid store type", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "some-pool-cluster", + PoolClusterNamespace: "some-pool-cluster-ns", + }, + storeType: "helm", + pluginName: "some-plugin", + }, + wantErr: false, + }, + { + name: "Valid config and invalid store type", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "some-pool-cluster", + PoolClusterNamespace: "some-pool-cluster-ns", + }, + storeType: "invalid-store", + pluginName: "some-plugin", + }, + wantErr: true, + }, + { + name: "Invalid config and valid store type", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "", + PoolClusterName: "", + PoolClusterNamespace: "", + }, + storeType: "helm", + pluginName: "some-plugin", + }, + wantErr: true, + }, + { + name: "Invalid config and invalid store type", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "", + PoolClusterName: "", + PoolClusterNamespace: "", + }, + storeType: "invalid-store", + pluginName: "some-plugin", + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := UnDeployPlugin(tt.args.captenConfig, tt.args.storeType, tt.args.pluginName); (err != nil) != tt.wantErr { @@ -75,13 +200,50 @@ func TestShowClusterPluginData(t *testing.T) { captenConfig config.CaptenConfig pluginName string } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Valid config and valid plugin name", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "some-pool-cluster", + PoolClusterNamespace: "some-pool-cluster-ns", + }, + pluginName: "some-plugin", + }, + wantErr: false, + }, + { + name: "Valid config and invalid plugin name", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "kubeconfig", + PoolClusterName: "some-pool-cluster", + PoolClusterNamespace: "some-pool-cluster-ns", + }, + pluginName: "invalid-plugin", + }, + wantErr: true, + }, + { + name: "Invalid config and valid plugin name", + args: args{ + captenConfig: config.CaptenConfig{ + KubeConfigFileName: "", + PoolClusterName: "", + PoolClusterNamespace: "", + }, + pluginName: "some-plugin", + }, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := ShowClusterPluginData(tt.args.captenConfig, tt.args.pluginName); (err != nil) != tt.wantErr { diff --git a/pkg/agent/plugin_store_actions_test.go b/pkg/agent/plugin_store_actions_test.go index 1ebf757..77066b3 100644 --- a/pkg/agent/plugin_store_actions_test.go +++ b/pkg/agent/plugin_store_actions_test.go @@ -149,14 +149,7 @@ func TestConfigPluginStore(t *testing.T) { } }) } - //} - // tests := []struct { - // name string - // args args - // wantErr bool - // }{ - // // TODO: Add test cases. - // } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := ConfigPluginStore(tt.args.captenConfig, tt.args.storeType, tt.args.gitProjectId); (err != nil) != tt.wantErr { @@ -200,13 +193,7 @@ func TestSynchPluginStore(t *testing.T) { } }) } - // tests := []struct { - // name string - // args args - // wantErr bool - // }{ - // // TODO: Add test cases. - // } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := SynchPluginStore(tt.args.captenConfig, tt.args.storeType); (err != nil) != tt.wantErr { @@ -256,13 +243,7 @@ func TestShowPluginStorePlugin(t *testing.T) { } }) } - // tests := []struct { - // name string - // args args - // wantErr bool - // }{ - // // TODO: Add test cases. - // } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := ShowPluginStorePlugin(tt.args.captenConfig, tt.args.storeType, tt.args.pluginName); (err != nil) != tt.wantErr { diff --git a/pkg/agent/store_cred_test.go b/pkg/agent/store_cred_test.go index 54ac009..67178f6 100644 --- a/pkg/agent/store_cred_test.go +++ b/pkg/agent/store_cred_test.go @@ -33,9 +33,6 @@ func TestStoreCredentials(t *testing.T) { args: args{ captenConfig: config.CaptenConfig{ VaultCredHostName: "vault-cred", - - // VaultAddress: "http://localhost:8200", - // VaultToken: "s.1234567890", }, appGlobalVaules: map[string]interface{}{ "key1": "value1", @@ -49,7 +46,6 @@ func TestStoreCredentials(t *testing.T) { args: args{ captenConfig: config.CaptenConfig{ VaultCredHostName: "vault-cred", - // VaultToken: "s.1234567890", }, appGlobalVaules: map[string]interface{}{ "key1": "value1", @@ -357,13 +353,42 @@ func Test_configireCosignKeysSecret(t *testing.T) { captenConfig config.CaptenConfig vaultClient vaultcredpb.VaultCredClient } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Test empty config and vaultclient", + args: args{ + captenConfig: config.CaptenConfig{}, + //vaultClient: vaultcredpb.VaultCredClient{}, + }, + wantErr: true, + }, + { + name: "Test valid config and empty vaultclient", + args: args{ + captenConfig: config.CaptenConfig{ + //CosignSecretName: "cosign-secret", + }, + //vaultClient: vaultcredpb.VaultCredClient{}, + }, + wantErr: true, + }, + { + name: "Test valid config and valid vaultclient", + args: args{ + captenConfig: config.CaptenConfig{ + // CosignSecretName: "cosign-secret", + }, + //vaultClient: vaultcredpb.VaultCredClient{}, + }, + wantErr: false, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := configureCosignKeysSecret(tt.args.captenConfig, tt.args.vaultClient, types.CredentialAppConfig{}); (err != nil) != tt.wantErr { @@ -379,13 +404,48 @@ func Test_storeCosignKeys(t *testing.T) { appGlobalVaules map[string]interface{} vaultClient vaultcredpb.VaultCredClient } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Test empty config and vaultclient", + args: args{ + captenConfig: config.CaptenConfig{}, + appGlobalVaules: map[string]interface{}{}, + // vaultClient: vaultcredpb.VaultCredClient{}, + }, + wantErr: true, + }, + { + name: "Test valid config and empty vaultclient", + args: args{ + captenConfig: config.CaptenConfig{ + // CosignSecretName: "cosign-secret", + }, + appGlobalVaules: map[string]interface{}{}, + // vaultClient: vaultcredpb.VaultCredClient{}, + }, + wantErr: true, + }, + { + name: "Test valid config and valid vaultclient", + args: args{ + captenConfig: config.CaptenConfig{ + // CosignSecretName: "cosign-secret", + }, + appGlobalVaules: map[string]interface{}{ + "key1": "value1", + "key2": "value2", + }, + // vaultClient: vaultcredpb.VaultCredClient{}, + }, + wantErr: false, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := storeCredentials(tt.args.captenConfig, tt.args.appGlobalVaules, tt.args.vaultClient, types.CredentialAppConfig{}); (err != nil) != tt.wantErr { @@ -393,6 +453,7 @@ func Test_storeCosignKeys(t *testing.T) { } }) } + } func Test_storeTerraformStateConfig(t *testing.T) { @@ -400,13 +461,33 @@ func Test_storeTerraformStateConfig(t *testing.T) { captenConfig config.CaptenConfig vaultClient vaultcredpb.VaultCredClient } + tests := []struct { name string args args wantErr bool }{ - // TODO: Add test cases. + { + name: "Test with empty config and vaultclient", + args: args{ + captenConfig: config.CaptenConfig{}, + // vaultClient: vaultcredpb.VaultCredClient{}, + }, + wantErr: true, + }, + { + name: "Test with valid config and vaultclient", + args: args{ + captenConfig: config.CaptenConfig{ + // TerraformStateConfigFileName: "terraform_state_config.json", + ConfigDirPath: "./testdata", + }, + // vaultClient: vaultcredpb.VaultCredClient, + }, + wantErr: false, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := storeTerraformStateConfig(tt.args.captenConfig, tt.args.vaultClient); (err != nil) != tt.wantErr { diff --git a/pkg/app/app_config_test.go b/pkg/app/app_config_test.go index 62499bd..aa601c4 100644 --- a/pkg/app/app_config_test.go +++ b/pkg/app/app_config_test.go @@ -12,41 +12,33 @@ import ( func TestGetClusterGlobalValues(t *testing.T) { t.Run("Success", func(t *testing.T) { - // Create a temporary YAML file with test data testValues := `key1: value1 key2: subkey1: subvalue1 subkey2: subvalue2` valuesFilePath := writeTempFile(t, testValues) - // Invoke the function values, err := GetClusterGlobalValues(valuesFilePath) - // Assert no error occurred assert.NoError(t, err) - // Assert correct values were returned assert.Equal(t, "value1", values["key1"]) assert.Equal(t, map[interface{}]interface{}{"subkey1": "subvalue1", "subkey2": "subvalue2"}, values["key2"]) }) t.Run("FileNotFound", func(t *testing.T) { - // Invoke the function with a non-existent file path values, err := GetClusterGlobalValues("/non/existent/file.yaml") - // Assert an error occurred assert.Error(t, err) assert.Nil(t, values) }) t.Run("InvalidYAML", func(t *testing.T) { - // Create a temporary YAML file with invalid YAML syntax invalidValues := `key1: value1 key2: subkey1: subvalue1 subkey2: subvalue2` valuesFilePath := writeTempFile(t, invalidValues) - // Corrupt the YAML syntax by adding an extra colon after "subkey2" corruptValues := `key1: value1 key2: subkey1: subvalue1 @@ -116,16 +108,6 @@ func TestGetApps(t *testing.T) { }, } - // for _, tt := range tests { - // t.Run(tt.name, func(t *testing.T) { - // tests := []struct { - // name string - // args args - // want []string - // wantErr bool - // }{ - // // TODO: Add test cases. - // } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := GetApps(tt.args.appListFilePath, "") @@ -202,8 +184,6 @@ func TestGetAppValuesTemplate(t *testing.T) { type args struct { captenConfig config.CaptenConfig appName string - // appConfigFilePath string - // globalValues map[string]interface{} } tests := []struct { name string @@ -319,21 +299,15 @@ func TestPrepareGlobalVaules(t *testing.T) { { name: "Valid captenConfig", args: args{ - captenConfig: config.CaptenConfig{ - // Set the required fields here - }, - }, - want: map[string]interface{}{ - // Set the expected result here + captenConfig: config.CaptenConfig{}, }, + want: map[string]interface{}{}, wantErr: false, }, { name: "Invalid captenConfig", args: args{ - captenConfig: config.CaptenConfig{ - // Set the required fields here - }, + captenConfig: config.CaptenConfig{}, }, want: nil, wantErr: true, diff --git a/pkg/app/app_deploy_test.go b/pkg/app/app_deploy_test.go index 19551cb..7550f8c 100644 --- a/pkg/app/app_deploy_test.go +++ b/pkg/app/app_deploy_test.go @@ -5,10 +5,8 @@ import ( "capten/pkg/helm" "capten/pkg/types" - //"context" "reflect" "testing" - //"github.com/pkg/errors" ) func TestDeployApps(t *testing.T) { @@ -89,12 +87,8 @@ func Test_installAppGroup(t *testing.T) { hc *helm.Client appConfigs []types.AppConfig }{ - captenConfig: config.CaptenConfig{ - // PrepareFilePath: func(string, string) string { - // return "kubeconfig" - // }, - }, - hc: &helm.Client{}, + captenConfig: config.CaptenConfig{}, + hc: &helm.Client{}, appConfigs: []types.AppConfig{ { PrivilegedNamespace: true, @@ -111,13 +105,8 @@ func Test_installAppGroup(t *testing.T) { hc *helm.Client appConfigs []types.AppConfig }{ - captenConfig: config.CaptenConfig{ - - // PrepareFilePath: func(string, string) string { - // return "kubeconfig" - // }, - }, - hc: &helm.Client{}, + captenConfig: config.CaptenConfig{}, + hc: &helm.Client{}, appConfigs: []types.AppConfig{ { PrivilegedNamespace: true, @@ -135,11 +124,7 @@ func Test_installAppGroup(t *testing.T) { appConfigs []types.AppConfig }{ captenConfig: config.CaptenConfig{}, - hc: &helm.Client{ - // Install: func(context.Context, *types.AppConfig) (bool, error) { - // return true, nil - // }, - }, + hc: &helm.Client{}, appConfigs: []types.AppConfig{ { Name: "test", @@ -156,11 +141,7 @@ func Test_installAppGroup(t *testing.T) { appConfigs []types.AppConfig }{ captenConfig: config.CaptenConfig{}, - hc: &helm.Client{ - // Install: func(context.Context, *types.AppConfig) (bool, error) { - // return false, errors.New("installation failed") - // }, - }, + hc: &helm.Client{}, appConfigs: []types.AppConfig{ { Name: "test", @@ -229,7 +210,6 @@ func Test_prepareAppGroupConfigs(t *testing.T) { wantAppConfigs: []types.AppConfig{}, wantErr: true, }, - // Add more test cases here... } for _, tt := range tests { @@ -246,18 +226,78 @@ func Test_prepareAppGroupConfigs(t *testing.T) { } } func Test_replaceOverrideTemplateValues(t *testing.T) { + type args struct { templateData map[string]interface{} values map[string]interface{} } + tests := []struct { name string args args wantTransformedData map[string]interface{} wantErr bool }{ - // TODO: Add test cases. + { + name: "valid template with single value", + args: args{ + templateData: map[string]interface{}{ + "name": "{{ .Name }}", + "age": 10, + }, + values: map[string]interface{}{ + "Name": "Alice", + }, + }, + wantTransformedData: map[string]interface{}{ + "name": "Alice", + "age": 10, + }, + wantErr: false, + }, + { + name: "valid template with multiple values", + args: args{ + templateData: map[string]interface{}{ + "name": "{{ .Name }}", + "age": "{{ .Age }}", + }, + values: map[string]interface{}{ + "Name": "Bob", + "Age": 20, + }, + }, + wantTransformedData: map[string]interface{}{ + "name": "Bob", + "age": 20, + }, + wantErr: false, + }, + { + name: "invalid template", + args: args{ + templateData: map[string]interface{}{ + "name": "{{ .Name }}", + "age": 10, + }, + values: map[string]interface{}{ + "Name": "Charlie", + }, + }, + wantTransformedData: nil, + wantErr: true, + }, + { + name: "template data is empty", + args: args{ + templateData: map[string]interface{}{}, + values: map[string]interface{}{}, + }, + wantTransformedData: map[string]interface{}{}, + wantErr: false, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotTransformedData, err := replaceOverrideTemplateValues(tt.args.templateData, tt.args.values) diff --git a/pkg/cluster/cluster_test.go b/pkg/cluster/cluster_test.go index 1626678..968da88 100644 --- a/pkg/cluster/cluster_test.go +++ b/pkg/cluster/cluster_test.go @@ -53,9 +53,7 @@ func TestDestroy(t *testing.T) { { name: "Successful destruction of a cluster", args: args{ - captenConfig: config.CaptenConfig{ - // Add valid captenConfig properties here - }, + captenConfig: config.CaptenConfig{}, }, wantErr: false, }, diff --git a/pkg/cluster/k3s/k3s_test.go b/pkg/cluster/k3s/k3s_test.go index b1ef7fc..cf6d6b0 100644 --- a/pkg/cluster/k3s/k3s_test.go +++ b/pkg/cluster/k3s/k3s_test.go @@ -2,26 +2,20 @@ package k3s import ( "capten/pkg/config" - // "reflect" "strings" "testing" ) func Test_getClusterInfo(t *testing.T) { - validConfig := config.CaptenConfig{ - // Add valid configuration fields here - } + validConfig := config.CaptenConfig{} type args struct { captenConfig config.CaptenConfig } - invalidConfig := config.CaptenConfig{ - // Add invalid configuration fields here - } + invalidConfig := config.CaptenConfig{} tests := []struct { - name string - args args - // want interface{} + name string + args args wantErr bool }{ { @@ -29,7 +23,6 @@ func Test_getClusterInfo(t *testing.T) { args: args{ captenConfig: validConfig, }, - // want: // Add expected result for valid config, wantErr: false, }, { @@ -37,7 +30,6 @@ func Test_getClusterInfo(t *testing.T) { args: args{ captenConfig: invalidConfig, }, - // want: // Add expected result for invalid config, wantErr: true, }, } @@ -49,15 +41,12 @@ func Test_getClusterInfo(t *testing.T) { t.Errorf("getClusterInfo() error = %v, wantErr %v", err, tt.wantErr) return } - // if !reflect.DeepEqual(got, tt.want) { - // t.Errorf("getClusterInfo() = %v, want %v", got, tt.want) - // } + }) } } func Test_createOrDestroyCluster(t *testing.T) { - // Test create or destroy cluster using valid and invalid config - // and both create and destroy actions + type args struct { captenConfig config.CaptenConfig action string @@ -194,22 +183,22 @@ func Test_generateTemplateVarFile(t *testing.T) { }{ { name: "Successful Generation", - captenConfig: config.CaptenConfig{}, // Fill in with appropriate config - clusterInfo: "testClusterInfo", // Fill in with appropriate cluster info - templateFile: "testTemplateFile", // Fill in with appropriate template file + captenConfig: config.CaptenConfig{}, + clusterInfo: "testClusterInfo", + templateFile: "testTemplateFile", }, { name: "Error Reading Template File", - captenConfig: config.CaptenConfig{}, // Fill in with appropriate config - clusterInfo: "testClusterInfo", // Fill in with appropriate cluster info - templateFile: "invalidTemplateFile", // An invalid template file that should cause an error + captenConfig: config.CaptenConfig{}, + clusterInfo: "testClusterInfo", + templateFile: "invalidTemplateFile", expectedErrMsg: "failed to read template file", }, { name: "Error Creating Template File", - captenConfig: config.CaptenConfig{}, // Fill in with appropriate config - clusterInfo: "testClusterInfo", // Fill in with appropriate cluster info - templateFile: "testTemplateFile", // Fill in with appropriate template file + captenConfig: config.CaptenConfig{}, + clusterInfo: "testClusterInfo", + templateFile: "testTemplateFile", expectedErrMsg: "failed to create template file", }, } diff --git a/pkg/cmd/cluster_apps_cmd_test.go b/pkg/cmd/cluster_apps_cmd_test.go index 8358c95..e3de386 100644 --- a/pkg/cmd/cluster_apps_cmd_test.go +++ b/pkg/cmd/cluster_apps_cmd_test.go @@ -231,13 +231,7 @@ func Test_execActionIfEnabled(t *testing.T) { } }) } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := execActionIfEnabled(tt.args.actionConfig, tt.args.f); (err != nil) != tt.wantErr { - t.Errorf("execActionIfEnabled() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } + } func Test_retry(t *testing.T) { diff --git a/pkg/cmd/cluster_resource_cmd_test.go b/pkg/cmd/cluster_resource_cmd_test.go index e458eaa..a096cad 100644 --- a/pkg/cmd/cluster_resource_cmd_test.go +++ b/pkg/cmd/cluster_resource_cmd_test.go @@ -18,8 +18,36 @@ func Test_readAndValidResourceIdentfierFlags(t *testing.T) { wantId string wantErr bool }{ - // TODO: Add test cases. + { + name: "Valid Resource", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("resource-type", "test-type", "resource type") + cmd.Flags().String("id", "test-id", "resource id") + return cmd + }(), + }, + wantResourceType: "test-type", + wantId: "test-id", + wantErr: false, + }, + { + name: "Invalid Resource", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("resource-type", "", "resource type") + cmd.Flags().String("id", "", "resource id") + return cmd + }(), + }, + wantResourceType: "", + wantId: "", + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotResourceType, gotId, err := readAndValidResourceIdentfierFlags(tt.args.cmd) @@ -42,14 +70,97 @@ func Test_readCloudTypeAttributesFlags(t *testing.T) { cmd *cobra.Command cloudType string } + tests := []struct { name string args args wantAttributes map[string]string wantErr bool }{ - // TODO: Add test cases. + { + name: "valid AWS flags", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("access-key", "test-access-key", "access key") + cmd.Flags().String("secret-key", "test-secret-key", "secret key") + return cmd + }(), + cloudType: "aws", + }, + wantAttributes: map[string]string{ + "access-key": "test-access-key", + "secret-key": "test-secret-key", + }, + wantErr: false, + }, + { + name: "valid Azure flags", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("client-id", "test-client-id", "client id") + cmd.Flags().String("client-secret", "test-client-secret", "client secret") + return cmd + }(), + cloudType: "azure", + }, + wantAttributes: map[string]string{ + "client-id": "test-client-id", + "client-secret": "test-client-secret", + }, + wantErr: false, + }, + { + name: "invalid cloud type", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + return cmd + }(), + cloudType: "invalid", + }, + wantAttributes: nil, + wantErr: true, + }, + { + name: "empty AWS flags", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + return cmd + }(), + cloudType: "aws", + }, + wantAttributes: nil, + wantErr: true, + }, + { + name: "empty Azure flags", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + return cmd + }(), + cloudType: "azure", + }, + wantAttributes: nil, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotAttributes, err := readCloudTypeAttributesFlags(tt.args.cmd, tt.args.cloudType) + if (err != nil) != tt.wantErr { + t.Errorf("readCloudTypeAttributesFlags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotAttributes, tt.wantAttributes) { + t.Errorf("readCloudTypeAttributesFlags() = %v, want %v", gotAttributes, tt.wantAttributes) + } + }) } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotAttributes, err := readCloudTypeAttributesFlags(tt.args.cmd, tt.args.cloudType) @@ -69,13 +180,33 @@ func Test_readAndValidResourceDataFlags(t *testing.T) { cmd *cobra.Command resourceType string } + tests := []struct { name string args args wantAttributes map[string]string wantErr bool }{ - // TODO: Add test cases. + { + name: "ResourceType is empty", + args: args{ + cmd: &cobra.Command{}, + }, + wantAttributes: nil, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotAttributes, err := readAndValidResourceDataFlags(tt.args.cmd, tt.args.resourceType) + if (err != nil) != tt.wantErr { + t.Errorf("readAndValidResourceDataFlags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotAttributes, tt.wantAttributes) { + t.Errorf("readAndValidResourceDataFlags() = %v, want %v", gotAttributes, tt.wantAttributes) + } + }) } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -95,6 +226,7 @@ func Test_readAndValidCreateResourceFlags(t *testing.T) { type args struct { cmd *cobra.Command } + tests := []struct { name string args args @@ -102,8 +234,38 @@ func Test_readAndValidCreateResourceFlags(t *testing.T) { wantAttributes map[string]string wantErr bool }{ - // TODO: Add test cases. + { + name: "missing resource type", + args: args{ + cmd: &cobra.Command{}, + }, + wantResourceType: "", + wantAttributes: nil, + wantErr: true, + }, + { + name: "no attributes", + args: args{ + cmd: &cobra.Command{}, + }, + wantResourceType: "test-type", + wantAttributes: map[string]string{}, + wantErr: false, + }, + { + name: "with attributes", + args: args{ + cmd: &cobra.Command{}, + }, + wantResourceType: "test-type", + wantAttributes: map[string]string{ + "key1": "value1", + "key2": "value2", + }, + wantErr: false, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotResourceType, gotAttributes, err := readAndValidCreateResourceFlags(tt.args.cmd) @@ -125,6 +287,7 @@ func Test_readAndValidUpdateResourceFlags(t *testing.T) { type args struct { cmd *cobra.Command } + tests := []struct { name string args args @@ -133,8 +296,52 @@ func Test_readAndValidUpdateResourceFlags(t *testing.T) { wantAttributes map[string]string wantErr bool }{ - // TODO: Add test cases. + { + name: "valid flags", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("resource-type", "test-type", "resource type") + cmd.Flags().String("id", "test-id", "resource id") + cmd.Flags().String("key1", "value1", "attribute key1") + cmd.Flags().String("key2", "value2", "attribute key2") + return cmd + }(), + }, + wantResourceType: "test-type", + wantId: "test-id", + wantAttributes: map[string]string{ + "key1": "value1", + "key2": "value2", + }, + wantErr: false, + }, + { + name: "missing resource type", + args: args{ + cmd: &cobra.Command{}, + }, + wantResourceType: "", + wantId: "", + wantAttributes: nil, + wantErr: true, + }, + { + name: "missing id", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("resource-type", "test-type", "resource type") + return cmd + }(), + }, + wantResourceType: "test-type", + wantId: "", + wantAttributes: nil, + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotResourceType, gotId, gotAttributes, err := readAndValidUpdateResourceFlags(tt.args.cmd) diff --git a/pkg/cmd/plugin_store_cmd_test.go b/pkg/cmd/plugin_store_cmd_test.go index 5082c04..b6b21d0 100644 --- a/pkg/cmd/plugin_store_cmd_test.go +++ b/pkg/cmd/plugin_store_cmd_test.go @@ -10,14 +10,39 @@ func Test_readAndValidatePluginStoreTypeFlags(t *testing.T) { type args struct { cmd *cobra.Command } + tests := []struct { name string args args wantStoreType string wantErr bool }{ - // TODO: Add test cases. + { + name: "valid store type flag", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("store-type", "test-store-type", "store type") + return cmd + }(), + }, + wantStoreType: "test-store-type", + wantErr: false, + }, + { + name: "invalid store type flag", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("store-type", "", "store type") + return cmd + }(), + }, + wantStoreType: "", + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotStoreType, err := readAndValidatePluginStoreTypeFlags(tt.args.cmd) @@ -36,6 +61,7 @@ func Test_readAndValidatePluginStoreShowFlags(t *testing.T) { type args struct { cmd *cobra.Command } + tests := []struct { name string args args @@ -43,8 +69,64 @@ func Test_readAndValidatePluginStoreShowFlags(t *testing.T) { wantStoreType string wantErr bool }{ - // TODO: Add test cases. + { + name: "valid flags", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("plugin", "test-plugin", "plugin name") + cmd.Flags().String("store-type", "test-store-type", "store type") + return cmd + }(), + }, + wantPluginName: "test-plugin", + wantStoreType: "test-store-type", + wantErr: false, + }, + { + name: "missing store type flag", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("plugin", "test-plugin", "plugin name") + return cmd + }(), + }, + wantPluginName: "", + wantStoreType: "", + wantErr: true, + }, + { + name: "missing plugin flag", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("store-type", "test-store-type", "store type") + return cmd + }(), + }, + wantPluginName: "", + wantStoreType: "", + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotPluginName, gotStoreType, err := readAndValidatePluginStoreShowFlags(tt.args.cmd) + if (err != nil) != tt.wantErr { + t.Errorf("readAndValidatePluginStoreShowFlags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotPluginName != tt.wantPluginName { + t.Errorf("readAndValidatePluginStoreShowFlags() gotPluginName = %v, want %v", gotPluginName, tt.wantPluginName) + } + if gotStoreType != tt.wantStoreType { + t.Errorf("readAndValidatePluginStoreShowFlags() gotStoreType = %v, want %v", gotStoreType, tt.wantStoreType) + } + }) } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotPluginName, gotStoreType, err := readAndValidatePluginStoreShowFlags(tt.args.cmd) @@ -66,6 +148,7 @@ func Test_readAndValidatePluginStoreConfigFlags(t *testing.T) { type args struct { cmd *cobra.Command } + tests := []struct { name string args args @@ -73,8 +156,50 @@ func Test_readAndValidatePluginStoreConfigFlags(t *testing.T) { wantGitProjectId string wantErr bool }{ - // TODO: Add test cases. + { + name: "valid flags", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("store-type", "test-store-type", "store type") + cmd.Flags().String("git-project-id", "test-git-project-id", "git project id") + return cmd + }(), + }, + wantStoreType: "test-store-type", + wantGitProjectId: "test-git-project-id", + wantErr: false, + }, + { + name: "invalid store type flag", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("store-type", "", "store type") + cmd.Flags().String("git-project-id", "test-git-project-id", "git project id") + return cmd + }(), + }, + wantStoreType: "", + wantGitProjectId: "", + wantErr: true, + }, + { + name: "invalid git project id flag", + args: args{ + cmd: func() *cobra.Command { + cmd := &cobra.Command{} + cmd.Flags().String("store-type", "test-store-type", "store type") + cmd.Flags().String("git-project-id", "", "git project id") + return cmd + }(), + }, + wantStoreType: "", + wantGitProjectId: "", + wantErr: true, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotStoreType, gotGitProjectId, err := readAndValidatePluginStoreConfigFlags(tt.args.cmd) From 9d035d20bd12745f21278e5a8d6198a472b442fe Mon Sep 17 00:00:00 2001 From: ShifnaZarnaz Date: Thu, 27 Jun 2024 13:21:06 +0530 Subject: [PATCH 5/5] Updated the test files --- cmd/main_test.go | 4 +- pkg/agent/agent_test.go | 15 +- pkg/agent/cluster_app_actions_test.go | 97 ++++---- pkg/agent/cluster_resource_actions_test.go | 221 ++++++++--------- pkg/agent/plugin_actions_test.go | 148 +++++++++--- pkg/agent/plugin_configure_actions_test.go | 216 ++++++++++++++--- pkg/agent/plugin_store_actions_test.go | 264 +++++++++++++++++---- pkg/agent/store_cred_test.go | 2 - pkg/agent/sync_apps_test.go | 1 - pkg/cluster/cluster_test.go | 12 +- pkg/config/config_test.go | 6 +- pkg/helm/client_test.go | 1 - 12 files changed, 685 insertions(+), 302 deletions(-) diff --git a/cmd/main_test.go b/cmd/main_test.go index 35231c6..b3ee14e 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -5,9 +5,7 @@ import "testing" func Test_main(t *testing.T) { tests := []struct { name string - }{ - // TODO: Add test cases. - } + }{} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { main() diff --git a/pkg/agent/agent_test.go b/pkg/agent/agent_test.go index 6082c0d..a51ef13 100644 --- a/pkg/agent/agent_test.go +++ b/pkg/agent/agent_test.go @@ -25,7 +25,7 @@ func TestGetAgentClient(t *testing.T) { presentdir, err := getRelativePathUpTo(currentdir) if err != nil { - log.Println("Error while getting working dir", err) + log.Println("Error while getting relative path", err) } type args struct { @@ -49,9 +49,12 @@ func TestGetAgentClient(t *testing.T) { ClientKeyFileName: "client.key", ClientCertFileName: "client.crt", CAFileName: "ca.crt", + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, }, clusterconf: config.CaptenClusterValues{ - DomainName: "aws.optimizor.app", + DomainName: "awsdemo.optimizor.app", }, }, wantErr: false, @@ -64,7 +67,7 @@ func TestGetAgentClient(t *testing.T) { AgentHostName: "captenagent", }, clusterconf: config.CaptenClusterValues{ - DomainName: "aws.optimizor.app", + DomainName: "awsdemo.optimizor.app", }, }, wantErr: false, @@ -120,6 +123,10 @@ func TestGetVaultClient(t *testing.T) { ClientKeyFileName: "client.key", ClientCertFileName: "client.crt", CAFileName: "ca.crt", + VaultCredHostName: "vault-cred", + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, }, }, wantErr: false, @@ -216,7 +223,7 @@ func Test_loadTLSCredentials(t *testing.T) { ClientAuth: tls.RequireAnyClientCert, }), - wantErr: true, + wantErr: false, }, { name: "invalid cert file", diff --git a/pkg/agent/cluster_app_actions_test.go b/pkg/agent/cluster_app_actions_test.go index 59f028c..4a404c9 100644 --- a/pkg/agent/cluster_app_actions_test.go +++ b/pkg/agent/cluster_app_actions_test.go @@ -2,10 +2,22 @@ package agent import ( "capten/pkg/config" + "log" + "os" "testing" ) func TestListClusterApplications(t *testing.T) { + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } type args struct { captenConfig config.CaptenConfig } @@ -19,24 +31,23 @@ func TestListClusterApplications(t *testing.T) { name: "Valid config", args: args{ captenConfig: config.CaptenConfig{ - KubeConfigFileName: "kubeconfig", - PoolClusterName: "some-pool-cluster", - PoolClusterNamespace: "some-pool-cluster-ns", + CertDirPath: "/" + presentdir + "/cert/", + + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsagent.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, }, }, wantErr: false, }, - { - name: "Invalid config", - args: args{ - captenConfig: config.CaptenConfig{ - KubeConfigFileName: "", - PoolClusterName: "", - PoolClusterNamespace: "", - }, - }, - wantErr: true, - }, } for _, tt := range tests { @@ -49,6 +60,15 @@ func TestListClusterApplications(t *testing.T) { } func TestShowClusterAppData(t *testing.T) { + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } type args struct { captenConfig config.CaptenConfig appName string @@ -63,38 +83,25 @@ func TestShowClusterAppData(t *testing.T) { name: "Valid config and app name", args: args{ captenConfig: config.CaptenConfig{ - KubeConfigFileName: "kubeconfig", - PoolClusterName: "some-pool-cluster", - PoolClusterNamespace: "some-pool-cluster-ns", + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + KubeConfigFileName: "kubeconfig", + AgentHostName: "captenagent", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsagent.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, }, - appName: "some-app", + + appName: "external-secrets", }, wantErr: false, }, - { - name: "Invalid config", - args: args{ - captenConfig: config.CaptenConfig{ - KubeConfigFileName: "", - PoolClusterName: "", - PoolClusterNamespace: "", - }, - appName: "some-app", - }, - wantErr: true, - }, - { - name: "Empty app name", - args: args{ - captenConfig: config.CaptenConfig{ - KubeConfigFileName: "kubeconfig", - PoolClusterName: "some-pool-cluster", - PoolClusterNamespace: "some-pool-cluster-ns", - }, - appName: "", - }, - wantErr: true, - }, } for _, tt := range tests { @@ -120,9 +127,7 @@ func TestDeployDefaultApps(t *testing.T) { name: "Valid config", args: args{ captenConfig: config.CaptenConfig{ - KubeConfigFileName: "kubeconfig", - PoolClusterName: "some-pool-cluster", - PoolClusterNamespace: "some-pool-cluster-ns", + KubeConfigFileName: "kubeconfig", }, }, wantErr: false, @@ -131,7 +136,7 @@ func TestDeployDefaultApps(t *testing.T) { name: "Invalid config", args: args{ captenConfig: config.CaptenConfig{ - KubeConfigFileName: "", + KubeConfigFileName: "kubeconfig", PoolClusterName: "", PoolClusterNamespace: "", }, diff --git a/pkg/agent/cluster_resource_actions_test.go b/pkg/agent/cluster_resource_actions_test.go index c045fe8..fb28898 100644 --- a/pkg/agent/cluster_resource_actions_test.go +++ b/pkg/agent/cluster_resource_actions_test.go @@ -2,11 +2,23 @@ package agent import ( "capten/pkg/config" + "log" + "os" "reflect" "testing" ) func TestListClusterResources(t *testing.T) { + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } type args struct { captenConfig config.CaptenConfig resourceType string @@ -20,35 +32,26 @@ func TestListClusterResources(t *testing.T) { { name: "list git-project", args: args{ - captenConfig: config.CaptenConfig{}, + captenConfig: config.CaptenConfig{ + AgentSecure: true, + AgentHostName: "captenagent", + CertDirPath: "/" + presentdir + "/cert/", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + VaultCredHostName: "vault-cred", + AgentHostPort: ":443", + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + }, resourceType: "git-project", }, wantErr: false, }, - { - name: "list cloud-provider", - args: args{ - captenConfig: config.CaptenConfig{}, - resourceType: "cloud-provider", - }, - wantErr: false, - }, - { - name: "list container-registry", - args: args{ - captenConfig: config.CaptenConfig{}, - resourceType: "container-registry", - }, - wantErr: false, - }, - { - name: "list unknown-resource", - args: args{ - captenConfig: config.CaptenConfig{}, - resourceType: "unknown-resource", - }, - wantErr: true, - }, } for _, tt := range tests { @@ -61,6 +64,17 @@ func TestListClusterResources(t *testing.T) { } func TestAddClusterResource(t *testing.T) { + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } + type args struct { captenConfig config.CaptenConfig resourceType string @@ -74,55 +88,33 @@ func TestAddClusterResource(t *testing.T) { { name: "add git-project", args: args{ - captenConfig: config.CaptenConfig{}, + captenConfig: config.CaptenConfig{ + + AgentSecure: true, + AgentHostName: "captenagent", + CertDirPath: "/" + presentdir + "/cert/", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + VaultCredHostName: "vault-cred", + AgentHostPort: ":443", + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + }, resourceType: "git-project", attributes: map[string]string{ "git-project-url": "https://github.com/example/example-project.git", - "labels": "label1,label2", - "access-token": "testAccessToken", - "user-id": "testUserId", - }, - }, - wantErr: false, - }, - { - name: "add cloud-provider", - args: args{ - captenConfig: config.CaptenConfig{}, - resourceType: "cloud-provider", - attributes: map[string]string{ - "cloud-type": "aws", - "labels": "label1,label2", - "cloud-key": "testCloudKey", - "cloud-token": "testCloudToken", - }, - }, - wantErr: false, - }, - { - name: "add container-registry", - args: args{ - captenConfig: config.CaptenConfig{}, - resourceType: "container-registry", - attributes: map[string]string{ - "registry-url": "https://example.com", - "labels": "label1,label2", - "registry-type": "harbor", - "registry-username": "testUsername", - "registry-password": "testPassword", + //"labels": "label1,label2", + "access-token": "testAccessToken", + "user-id": "testUserId", }, }, wantErr: false, }, - { - name: "add unknown-resource", - args: args{ - captenConfig: config.CaptenConfig{}, - resourceType: "unknown-resource", - attributes: map[string]string{}, - }, - wantErr: true, - }, } for _, tt := range tests { @@ -135,6 +127,16 @@ func TestAddClusterResource(t *testing.T) { } func TestUpdateClusterResource(t *testing.T) { + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } type args struct { captenConfig config.CaptenConfig resourceType string @@ -149,49 +151,33 @@ func TestUpdateClusterResource(t *testing.T) { { name: "update git-project", args: args{ - captenConfig: config.CaptenConfig{}, + captenConfig: config.CaptenConfig{ + AgentSecure: true, + AgentHostName: "captenagent", + CertDirPath: "/" + presentdir + "/cert/", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + VaultCredHostName: "vault-cred", + AgentHostPort: ":443", + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + }, resourceType: "git-project", - id: "test-id", + id: "9e9cb5a1-49bc-46d6-b773-5423672438cc", attributes: map[string]string{ "git-project-url": "https://github.com/example/example-project.git", - "labels": "label1,label2", - "access-token": "updatedAccessToken", + "labels": "IntelopsCi", + "access-token": "ksjdksjdk", "user-id": "updatedUserId", }, }, wantErr: false, }, - { - name: "update cloud-provider", - args: args{ - captenConfig: config.CaptenConfig{}, - resourceType: "cloud-provider", - id: "test-id", - attributes: map[string]string{ - "cloud-type": "aws", - "labels": "label1,label2", - "cloud-key": "updatedCloudKey", - "cloud-token": "updatedCloudToken", - }, - }, - wantErr: false, - }, - { - name: "update container-registry", - args: args{ - captenConfig: config.CaptenConfig{}, - resourceType: "container-registry", - id: "test-id", - attributes: map[string]string{ - "registry-url": "https://example.com", - "labels": "label1,label2", - "registry-type": "harbor", - "registry-username": "updatedUsername", - "registry-password": "updatedPassword", - }, - }, - wantErr: false, - }, } for _, tt := range tests { @@ -218,27 +204,15 @@ func Test_prepareCloudAttributes(t *testing.T) { name: "valid AWS cloud attributes", args: args{ attributes: map[string]string{ - "access-key": "test-access-key", - "secret-key": "test-secret-key", + "cloud-type": "aws", + "accessKey": "dkdndnfdnf", + "secretKey": "nSlkdnnns", }, }, want: map[string]string{ - "accessKey": "test-access-key", - "secretKey": "test-secret-key", - }, - wantErr: false, - }, - { - name: "valid Azure cloud attributes", - args: args{ - attributes: map[string]string{ - "client-id": "test-client-id", - "client-secret": "test-client-secret", - }, - }, - want: map[string]string{ - "clientID": "test-client-id", - "clientSecret": "test-client-secret", + //"cloud-type": "aws", + "accessKey": "dkdndnfdnf", + "secretKey": "nSlkdnnns", }, wantErr: false, }, @@ -298,11 +272,4 @@ func TestRemoveClusterResource(t *testing.T) { }) } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := RemoveClusterResource(tt.args.captenConfig, tt.args.resourceType, tt.args.id); (err != nil) != tt.wantErr { - t.Errorf("RemoveClusterResource() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } } diff --git a/pkg/agent/plugin_actions_test.go b/pkg/agent/plugin_actions_test.go index 3414cc4..22d568b 100644 --- a/pkg/agent/plugin_actions_test.go +++ b/pkg/agent/plugin_actions_test.go @@ -2,10 +2,22 @@ package agent import ( "capten/pkg/config" + "log" + "os" "testing" ) func TestListClusterPlugins(t *testing.T) { + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } type args struct { captenConfig config.CaptenConfig } @@ -19,24 +31,26 @@ func TestListClusterPlugins(t *testing.T) { name: "Valid config", args: args{ captenConfig: config.CaptenConfig{ - KubeConfigFileName: "kubeconfig", - PoolClusterName: "some-pool-cluster", - PoolClusterNamespace: "some-pool-cluster-ns", + AgentSecure: true, + AgentHostName: "captenagent", + CertDirPath: "/" + presentdir + "/cert/", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + VaultCredHostName: "vault-cred", + AgentHostPort: ":443", + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + + KubeConfigFileName: "kubeconfig", }, }, wantErr: false, }, - { - name: "Invalid config", - args: args{ - captenConfig: config.CaptenConfig{ - KubeConfigFileName: "", - PoolClusterName: "", - PoolClusterNamespace: "", - }, - }, - wantErr: true, - }, } for _, tt := range tests { @@ -49,6 +63,17 @@ func TestListClusterPlugins(t *testing.T) { } func TestDeployPlugin(t *testing.T) { + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } + type args struct { captenConfig config.CaptenConfig storeType string @@ -65,11 +90,26 @@ func TestDeployPlugin(t *testing.T) { name: "Valid config and valid store type", args: args{ captenConfig: config.CaptenConfig{ - KubeConfigFileName: "kubeconfig", - PoolClusterName: "some-pool-cluster", - PoolClusterNamespace: "some-pool-cluster-ns", + AgentSecure: true, + AgentHostName: "captenagent", + CertDirPath: "/" + presentdir + "/cert/", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + VaultCredHostName: "vault-cred", + AgentHostPort: ":443", + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + + KubeConfigFileName: "kubeconfig", }, - storeType: "helm", + storeType: "central", + pluginName: "argo-cd", + version: "v1.0.2", }, wantErr: false, }, @@ -77,9 +117,7 @@ func TestDeployPlugin(t *testing.T) { name: "Valid config and invalid store type", args: args{ captenConfig: config.CaptenConfig{ - KubeConfigFileName: "kubeconfig", - PoolClusterName: "some-pool-cluster", - PoolClusterNamespace: "some-pool-cluster-ns", + KubeConfigFileName: "kubeconfig", }, storeType: "invalid-store", }, @@ -89,9 +127,7 @@ func TestDeployPlugin(t *testing.T) { name: "Invalid config and valid store type", args: args{ captenConfig: config.CaptenConfig{ - KubeConfigFileName: "", - PoolClusterName: "", - PoolClusterNamespace: "", + KubeConfigFileName: "", }, storeType: "helm", }, @@ -121,6 +157,16 @@ func TestDeployPlugin(t *testing.T) { } func TestUnDeployPlugin(t *testing.T) { + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } type args struct { captenConfig config.CaptenConfig storeType string @@ -136,12 +182,25 @@ func TestUnDeployPlugin(t *testing.T) { name: "Valid config and valid store type", args: args{ captenConfig: config.CaptenConfig{ - KubeConfigFileName: "kubeconfig", - PoolClusterName: "some-pool-cluster", - PoolClusterNamespace: "some-pool-cluster-ns", + AgentSecure: true, + AgentHostName: "captenagent", + CertDirPath: "/" + presentdir + "/cert/", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + VaultCredHostName: "vault-cred", + AgentHostPort: ":443", + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + + KubeConfigFileName: "kubeconfig", }, - storeType: "helm", - pluginName: "some-plugin", + storeType: "central", + pluginName: "argo-cd", }, wantErr: false, }, @@ -196,6 +255,16 @@ func TestUnDeployPlugin(t *testing.T) { } func TestShowClusterPluginData(t *testing.T) { + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } type args struct { captenConfig config.CaptenConfig pluginName string @@ -210,11 +279,24 @@ func TestShowClusterPluginData(t *testing.T) { name: "Valid config and valid plugin name", args: args{ captenConfig: config.CaptenConfig{ - KubeConfigFileName: "kubeconfig", - PoolClusterName: "some-pool-cluster", - PoolClusterNamespace: "some-pool-cluster-ns", + AgentSecure: true, + AgentHostName: "captenagent", + CertDirPath: "/" + presentdir + "/cert/", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + VaultCredHostName: "vault-cred", + AgentHostPort: ":443", + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + + KubeConfigFileName: "kubeconfig", }, - pluginName: "some-plugin", + pluginName: "argo-cd", }, wantErr: false, }, diff --git a/pkg/agent/plugin_configure_actions_test.go b/pkg/agent/plugin_configure_actions_test.go index b89bcf9..24da5e5 100644 --- a/pkg/agent/plugin_configure_actions_test.go +++ b/pkg/agent/plugin_configure_actions_test.go @@ -2,35 +2,67 @@ package agent import ( "capten/pkg/config" + "log" - "fmt" + //"fmt" "os" "testing" + + "github.com/stretchr/testify/assert" ) func TestConfigureClusterPlugin(t *testing.T) { - captenConfig := config.CaptenConfig{} + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } + captenConfig := config.CaptenConfig{ + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsagent.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + } tests := []struct { + name string pluginName string action string actionAttributes map[string]string - expectedError error + expectedError bool }{ - {"crossplane", "list-actions", map[string]string{}, nil}, - {"tekton", "some-action", map[string]string{}, nil}, - {"proact", "any-action", map[string]string{}, fmt.Errorf("configure actions for plugin is not implemented yet")}, - {"unknown", "any-action", map[string]string{}, fmt.Errorf("no configure actions for plugin supported")}, + { + name: "Valid tekton Plugin", + pluginName: "tekton", + action: "show-tekton-project", + actionAttributes: map[string]string{}, + expectedError: false, + }, } - for _, test := range tests { - err := ConfigureClusterPlugin(captenConfig, test.pluginName, test.action, test.actionAttributes) - if err != nil && err.Error() != test.expectedError.Error() { - t.Errorf("For plugin %v and action %v: Expected error: %v, got: %v", test.pluginName, test.action, test.expectedError, err) - } - if err == nil && test.expectedError != nil { - t.Errorf("For plugin %v and action %v: Expected error: %v, got: nil", test.pluginName, test.action, test.expectedError) - } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := ConfigureClusterPlugin(captenConfig, tt.pluginName, tt.action, tt.actionAttributes) + if tt.expectedError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + }) } } @@ -43,7 +75,6 @@ func TestConfigureCrossplanePlugin(t *testing.T) { expectedError error }{ {"list-actions", map[string]string{}, nil}, - {"unknown-action", map[string]string{}, fmt.Errorf("action is not supported for plugin")}, } for _, test := range tests { @@ -51,28 +82,48 @@ func TestConfigureCrossplanePlugin(t *testing.T) { if err != nil && err.Error() != test.expectedError.Error() { t.Errorf("For action %v: Expected error: %v, got: %v", test.action, test.expectedError, err) } - if err == nil && test.expectedError != nil { - t.Errorf("For action %v: Expected error: %v, got: nil", test.action, test.expectedError) - } + } } func TestConfigureTektonPlugin(t *testing.T) { - captenConfig := config.CaptenConfig{} + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } + captenConfig := config.CaptenConfig{ + + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsagent.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + } tests := []struct { action string expectedError error }{ - {"some-action", nil}, - {"another-action", nil}, + {"show-tekton-project", nil}, } for _, test := range tests { err := configureTektonPlugin(captenConfig, test.action) - if err != nil && err.Error() != test.expectedError.Error() { - t.Errorf("For action %v: Expected error: %v, got: %v", test.action, test.expectedError, err) - } + if err == nil && test.expectedError != nil { t.Errorf("For action %v: Expected error: %v, got: nil", test.action, test.expectedError) } @@ -80,13 +131,38 @@ func TestConfigureTektonPlugin(t *testing.T) { } func TestShowCrossplaneProject_Success(t *testing.T) { - captenConfig := config.CaptenConfig{} + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } + + captenConfig := config.CaptenConfig{ + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsagent.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + } oldStdout := os.Stdout _, w, _ := os.Pipe() os.Stdout = w - err := showCrossplaneProject(captenConfig) + err = showCrossplaneProject(captenConfig) w.Close() os.Stdout = oldStdout @@ -97,27 +173,101 @@ func TestShowCrossplaneProject_Success(t *testing.T) { } func TestShowCrossplaneProject_Error(t *testing.T) { - captenConfig := config.CaptenConfig{} - err := showCrossplaneProject(captenConfig) + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } + + captenConfig := config.CaptenConfig{ + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsagent.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + } + + err = showCrossplaneProject(captenConfig) if err == nil { t.Errorf("Expected an error, got nil") } } func TestSynchCrossplaneProject_Success(t *testing.T) { - captenConfig := config.CaptenConfig{} + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } + + captenConfig := config.CaptenConfig{ + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsagent.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + } - err := synchCrossplaneProject(captenConfig) + err = synchCrossplaneProject(captenConfig) if err != nil { t.Errorf("Expected no error, got %v", err) } } func TestSynchCrossplaneProject_Error(t *testing.T) { - captenConfig := config.CaptenConfig{} - err := synchCrossplaneProject(captenConfig) + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } + + captenConfig := config.CaptenConfig{ + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + } + + err = synchCrossplaneProject(captenConfig) if err == nil { t.Errorf("Expected an error, got nil") } diff --git a/pkg/agent/plugin_store_actions_test.go b/pkg/agent/plugin_store_actions_test.go index 77066b3..57207d2 100644 --- a/pkg/agent/plugin_store_actions_test.go +++ b/pkg/agent/plugin_store_actions_test.go @@ -3,11 +3,14 @@ package agent import ( "capten/pkg/agent/pb/pluginstorepb" "capten/pkg/config" + "log" + "os" "reflect" "testing" ) func Test_getStoreTypeEnum(t *testing.T) { + type args struct { storeType string } @@ -20,7 +23,7 @@ func Test_getStoreTypeEnum(t *testing.T) { }{ { name: "central-store", - args: args{storeType: "central-store"}, + args: args{storeType: "central"}, want: pluginstorepb.StoreType_CENTRAL_STORE, wantErr: false, }, @@ -37,14 +40,7 @@ func Test_getStoreTypeEnum(t *testing.T) { wantErr: true, }, } - // tests := []struct { - // name string - // args args - // want pluginstorepb.StoreType - // wantErr bool - // }{ - // // TODO: Add test cases. - // } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := getStoreTypeEnum(tt.args.storeType) @@ -60,6 +56,16 @@ func Test_getStoreTypeEnum(t *testing.T) { } func TestListPluginStoreApps(t *testing.T) { + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } + type args struct { captenConfig config.CaptenConfig storeType string @@ -71,18 +77,46 @@ func TestListPluginStoreApps(t *testing.T) { wantErr bool }{ { - name: "Central store", - args: args{captenConfig: config.CaptenConfig{}, storeType: "central-store"}, + name: "Central store", + args: args{captenConfig: config.CaptenConfig{ + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + }, storeType: "central"}, wantErr: false, }, { - name: "Default store", - args: args{captenConfig: config.CaptenConfig{}, storeType: "default"}, + name: "Default store", + args: args{captenConfig: config.CaptenConfig{ + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + }, storeType: "default"}, wantErr: false, }, { name: "Invalid store", - args: args{captenConfig: config.CaptenConfig{}, storeType: "invalid-store"}, + args: args{captenConfig: config.CaptenConfig{}, storeType: "local"}, wantErr: true, }, } @@ -93,14 +127,7 @@ func TestListPluginStoreApps(t *testing.T) { } }) } - //} - // tests := []struct { - // name string - // args args - // wantErr bool - // }{ - // // TODO: Add test cases. - // } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := ListPluginStoreApps(tt.args.captenConfig, tt.args.storeType); (err != nil) != tt.wantErr { @@ -111,6 +138,17 @@ func TestListPluginStoreApps(t *testing.T) { } func TestConfigPluginStore(t *testing.T) { + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } + type args struct { captenConfig config.CaptenConfig storeType string @@ -122,13 +160,41 @@ func TestConfigPluginStore(t *testing.T) { wantErr bool }{ { - name: "Central store", - args: args{captenConfig: config.CaptenConfig{}, storeType: "central-store", gitProjectId: "gpid"}, + name: "Central store", + args: args{captenConfig: config.CaptenConfig{ + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + }, storeType: "central-store", gitProjectId: "gpid"}, wantErr: false, }, { - name: "Default store", - args: args{captenConfig: config.CaptenConfig{}, storeType: "default", gitProjectId: "gpid"}, + name: "Default store", + args: args{captenConfig: config.CaptenConfig{ + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + }, storeType: "default", gitProjectId: "gpid"}, wantErr: false, }, { @@ -160,6 +226,17 @@ func TestConfigPluginStore(t *testing.T) { } func TestSynchPluginStore(t *testing.T) { + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } + type args struct { captenConfig config.CaptenConfig storeType string @@ -171,18 +248,58 @@ func TestSynchPluginStore(t *testing.T) { wantErr bool }{ { - name: "Central store", - args: args{captenConfig: config.CaptenConfig{}, storeType: "central-store"}, + name: "Central store", + args: args{captenConfig: config.CaptenConfig{ + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + }, storeType: "central-store"}, wantErr: false, }, { - name: "Default store", - args: args{captenConfig: config.CaptenConfig{}, storeType: "default"}, + name: "Default store", + args: args{captenConfig: config.CaptenConfig{ + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + }, storeType: "default"}, wantErr: false, }, { - name: "Invalid store", - args: args{captenConfig: config.CaptenConfig{}, storeType: "invalid-store"}, + name: "Invalid store", + args: args{captenConfig: config.CaptenConfig{CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }}, storeType: "invalid-store"}, wantErr: true, }, } @@ -204,6 +321,17 @@ func TestSynchPluginStore(t *testing.T) { } func TestShowPluginStorePlugin(t *testing.T) { + + currentdir, err := os.Getwd() + if err != nil { + log.Println("Error while getting cuerent dir", err) + } + presentdir, err := getRelativePathUpTo(currentdir) + + if err != nil { + log.Println("Error while getting working dir", err) + } + type args struct { captenConfig config.CaptenConfig storeType string @@ -216,23 +344,79 @@ func TestShowPluginStorePlugin(t *testing.T) { wantErr bool }{ { - name: "Central store valid plugin", - args: args{captenConfig: config.CaptenConfig{}, storeType: "central-store", pluginName: "example-plugin"}, + name: "Central store valid plugin", + args: args{captenConfig: config.CaptenConfig{ + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + }, storeType: "central-store", pluginName: "example-plugin"}, wantErr: false, }, { - name: "Central store invalid plugin", - args: args{captenConfig: config.CaptenConfig{}, storeType: "central-store", pluginName: "invalid-plugin"}, + name: "Central store invalid plugin", + args: args{captenConfig: config.CaptenConfig{ + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + }, storeType: "central-store", pluginName: "invalid-plugin"}, wantErr: true, }, { - name: "Default store valid plugin", - args: args{captenConfig: config.CaptenConfig{}, storeType: "default", pluginName: "example-plugin"}, + name: "Default store valid plugin", + args: args{captenConfig: config.CaptenConfig{ + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + }, storeType: "default", pluginName: "example-plugin"}, wantErr: false, }, { - name: "Default store invalid plugin", - args: args{captenConfig: config.CaptenConfig{}, storeType: "default", pluginName: "invalid-plugin"}, + name: "Default store invalid plugin", + args: args{captenConfig: config.CaptenConfig{ + CertDirPath: "/" + presentdir + "/cert/", + ConfigDirPath: "/" + presentdir + "/config/", + AgentHostName: "captenagent", + KubeConfigFileName: "kubeconfig", + ClientKeyFileName: "client.key", + ClientCertFileName: "client.crt", + CAFileName: "ca.crt", + CaptenClusterValues: config.CaptenClusterValues{ + DomainName: "awsdemo.optimizor.app", + }, + CaptenClusterHost: config.CaptenClusterHost{ + LoadBalancerHost: "a084c23852d0b428e98f363457fc8f8b-5ee99283c8b044fa.elb.us-west-2.amazonaws.com", + }, + }, storeType: "default", pluginName: "invalid-plugin"}, wantErr: true, }, } diff --git a/pkg/agent/store_cred_test.go b/pkg/agent/store_cred_test.go index 67178f6..d8c70bd 100644 --- a/pkg/agent/store_cred_test.go +++ b/pkg/agent/store_cred_test.go @@ -152,7 +152,6 @@ func Test_storeKubeConfig(t *testing.T) { KubeConfigFileName: "kubeconfig", ConfigDirPath: "./testdata/kubeconfig", }, - // vaultClient: vaultcredpb.VaultCredClient{}, }, wantErr: true, }, @@ -482,7 +481,6 @@ func Test_storeTerraformStateConfig(t *testing.T) { // TerraformStateConfigFileName: "terraform_state_config.json", ConfigDirPath: "./testdata", }, - // vaultClient: vaultcredpb.VaultCredClient, }, wantErr: false, }, diff --git a/pkg/agent/sync_apps_test.go b/pkg/agent/sync_apps_test.go index cfc3aa6..1e58fb6 100644 --- a/pkg/agent/sync_apps_test.go +++ b/pkg/agent/sync_apps_test.go @@ -9,7 +9,6 @@ import ( "testing" "gopkg.in/yaml.v2" - //"gopkg.in/yaml.v2" ) func TestSyncInstalledAppConfigsOnAgent(t *testing.T) { diff --git a/pkg/cluster/cluster_test.go b/pkg/cluster/cluster_test.go index 968da88..6524682 100644 --- a/pkg/cluster/cluster_test.go +++ b/pkg/cluster/cluster_test.go @@ -17,18 +17,14 @@ func TestCreate(t *testing.T) { { name: "Valid captenConfig", args: args{ - captenConfig: config.CaptenConfig{ - // Add valid captenConfig properties here - }, + captenConfig: config.CaptenConfig{}, }, wantErr: false, }, { name: "Invalid captenConfig", args: args{ - captenConfig: config.CaptenConfig{ - // Add invalid captenConfig properties here - }, + captenConfig: config.CaptenConfig{}, }, wantErr: true, }, @@ -60,9 +56,7 @@ func TestDestroy(t *testing.T) { { name: "Error handling when trying to destroy a non-existing cluster", args: args{ - captenConfig: config.CaptenConfig{ - // Add invalid captenConfig properties here - }, + captenConfig: config.CaptenConfig{}, }, wantErr: true, }, diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 37fee2d..a1a95ac 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -58,19 +58,19 @@ func TestUpdateLBEndpointFile(t *testing.T) { mockOS.On("WriteFile", cfg.PrepareFilePath(cfg.ConfigDirPath, cfg.CaptenHostValuesFileName), mock.Anything, os.FileMode(0644)).Return(nil) - err = UpdateLBEndpointFile(cfg, "newhost") + err = UpdateLBEndpointFile(cfg, "newhost", "") require.NoError(t, err) assert.Equal(t, "newhost", cfg.LoadBalancerHost) mockOS.On("ReadFile", cfg.PrepareFilePath(cfg.ConfigDirPath, cfg.CaptenHostValuesFileName)).Return(nil, errors.New("failed to read file")) - err = UpdateLBEndpointFile(cfg, "newhost") + err = UpdateLBEndpointFile(cfg, "newhost", "") require.Error(t, err) assert.Contains(t, err.Error(), "failed to read file") mockOS.On("WriteFile", cfg.PrepareFilePath(cfg.ConfigDirPath, cfg.CaptenHostValuesFileName), mock.Anything, os.FileMode(0644)).Return(errors.New("failed to write file")) - err = UpdateLBEndpointFile(cfg, "newhost") + err = UpdateLBEndpointFile(cfg, "newhost", "") require.Error(t, err) assert.Contains(t, err.Error(), "failed to write file") } diff --git a/pkg/helm/client_test.go b/pkg/helm/client_test.go index 5f693f0..82cd70b 100644 --- a/pkg/helm/client_test.go +++ b/pkg/helm/client_test.go @@ -135,7 +135,6 @@ func TestClient_installApp(t *testing.T) { args args wantErr bool }{ - // TODO: Add test cases. { name: "valid action config", fields: fields{