Skip to content

Commit

Permalink
Fix tests and cleanup files
Browse files Browse the repository at this point in the history
  • Loading branch information
pkosiec committed Mar 3, 2023
1 parent e78ce9b commit 5c3f787
Show file tree
Hide file tree
Showing 31 changed files with 177 additions and 155 deletions.
1 change: 0 additions & 1 deletion cmd/botkube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func run(ctx context.Context) error {
// Load configuration
intconfig.RegisterFlags(pflag.CommandLine)

// TODO: Try to clean it up somehow
remoteCfgSyncEnabled := graphql.IsRemoteConfigEnabled()
gqlClient := graphql.NewDefaultGqlClient()
deployClient := intconfig.NewDeploymentClient(gqlClient)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ require (
k8s.io/kubectl v0.25.4
k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2
sigs.k8s.io/controller-runtime v0.13.1
sigs.k8s.io/yaml v1.3.0
)

require (
Expand Down Expand Up @@ -196,6 +195,7 @@ require (
sigs.k8s.io/kustomize/api v0.12.1 // indirect
sigs.k8s.io/kustomize/kyaml v0.13.9 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

go 1.19
3 changes: 2 additions & 1 deletion internal/config/env_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package config

import (
"context"
"github.com/kubeshop/botkube/pkg/config"
"os"
"strings"

"github.com/kubeshop/botkube/pkg/config"
)

const (
Expand Down
8 changes: 5 additions & 3 deletions internal/config/env_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

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

func TestEnvProviderSuccess(t *testing.T) {
Expand All @@ -14,19 +15,20 @@ func TestEnvProviderSuccess(t *testing.T) {

// when
p := NewEnvProvider()
configs, err := p.Configs(context.Background())
configs, cfgVer, err := p.Configs(context.Background())

// then
assert.NoError(t, err)
require.NoError(t, err)
content, err := os.ReadFile("testdata/TestEnvProviderSuccess/config.yaml")
assert.NoError(t, err)
assert.Equal(t, content, configs[0])
assert.Equal(t, cfgVer, 0)
}

func TestEnvProviderErr(t *testing.T) {
// when
p := NewEnvProvider()
_, err := p.Configs(context.Background())
_, _, err := p.Configs(context.Background())

// then
assert.Equal(t, "while reading a file: read .: is a directory", err.Error())
Expand Down
3 changes: 2 additions & 1 deletion internal/config/fs_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package config
import (
"context"
"fmt"
"github.com/kubeshop/botkube/pkg/config"
"os"
"path/filepath"
"strings"

"github.com/kubeshop/botkube/pkg/config"
)

const specialConfigFileNamePrefix = "_"
Expand Down
6 changes: 4 additions & 2 deletions internal/config/fs_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import (
"testing"

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

func TestStaticProviderSuccess(t *testing.T) {
// when
p := NewFileSystemProvider([]string{"testdata/TestStaticProviderSuccess/config.yaml"})
configs, err := p.Configs(context.Background())
configs, cfgVer, err := p.Configs(context.Background())

// then
assert.NoError(t, err)
require.NoError(t, err)
content, err := os.ReadFile("testdata/TestStaticProviderSuccess/config.yaml")
assert.NoError(t, err)
assert.Equal(t, content, configs[0])
assert.Equal(t, cfgVer, 0)
}

func TestSortCfgFiles(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/config/gql_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewDeploymentClient(client *gql.Gql) *Gql {
// Deployment returns deployment with Botkube configuration.
type Deployment struct {
ResourceVersion int
YAMLConfig string
YAMLConfig string
}

// GetDeployment retrieves deployment by id.
Expand Down
4 changes: 2 additions & 2 deletions internal/config/gql_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func TestGql_GetDeployment(t *testing.T) {
expectedBody := fmt.Sprintf(`{"query":"query ($id:ID!){deployment(id: $id){botkubeConfig}}","variables":{"id":"my-id"}}%s`, "\n")
expectedBody := fmt.Sprintf(`{"query":"query ($id:ID!){deployment(id: $id){resourceVersion,yamlConfig}}","variables":{"id":"my-id"}}%s`, "\n")
file, err := os.ReadFile("testdata/gql_get_deployment_success.json")
assert.NoError(t, err)
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -51,5 +51,5 @@ func TestGql_GetDeployment(t *testing.T) {
g := NewDeploymentClient(gqlClient)
deployment, err := g.GetDeployment(context.Background())
assert.NoError(t, err)
assert.NotNil(t, deployment.BotkubeConfig)
assert.NotNil(t, deployment.YAMLConfig)
}
3 changes: 2 additions & 1 deletion internal/config/gql_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package config

import (
"context"
"github.com/kubeshop/botkube/pkg/config"

"github.com/pkg/errors"

"github.com/kubeshop/botkube/pkg/config"
)

// GqlProvider is GraphQL provider
Expand Down
15 changes: 9 additions & 6 deletions internal/config/gql_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,33 @@ import (
"testing"

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

var _ DeploymentClient = &fakeGqlClient{}

type fakeGqlClient struct {
}

func (f *fakeGqlClient) GetDeployment(ctx context.Context) (Deployment, error) {
func (f *fakeGqlClient) GetDeployment(context.Context) (Deployment, error) {
return Deployment{
BotkubeConfig: "{\"settings\":{\"clusterName\":\"qa\"},\"sources\":{\"kubernetes-info\":{\"displayName\":\"Kubernetes Information\",\"kubernetes\":{\"recommendations\":{\"pod\":{\"noLatestImageTag\":true,\"labelsSet\":true},\"ingress\":{\"backendServiceValid\":true,\"tlsSecretValid\":true}}}}},\"executors\":{\"kubectl-read-only\":{\"kubectl\":{\"namespaces\":{\"include\":[\".*\"]},\"enabled\":true,\"commands\":{\"verbs\":[\"api-resources\",\"api-versions\",\"cluster-info\",\"describe\",\"diff\",\"explain\",\"get\",\"logs\",\"top\",\"auth\"],\"resources\":[\"deployments\",\"pods\",\"namespaces\",\"daemonsets\",\"statefulsets\",\"storageclasses\",\"nodes\"]},\"defaultNamespace\":\"default\",\"restrictAccess\":false}}},\"communications\":{\"default-group\":{\"socketSlack\":{\"enabled\":true,\"channels\":{\"botkube-demo\":{\"name\":\"botkube-demo\",\"notification\":{\"disabled\":false},\"bindings\":{\"sources\":[\"kubernetes-info\"],\"executors\":[\"kubectl-read-only\"]}}},\"botToken\":\"xoxb-3933899240838\",\"appToken\":\"xapp-1-A047D1ZJ03B-4262138376928\"}}}}",
ResourceVersion: 3,
YAMLConfig: "communications:\n default-group:\n socketSlack:\n appToken: xapp-1-A047D1ZJ03B-4262138376928\n botToken: xoxb-3933899240838\n channels:\n botkube-demo:\n bindings:\n executors:\n - kubectl-read-only\n sources:\n - kubernetes-info\n name: botkube-demo\n notification:\n disabled: false\n enabled: true\nexecutors:\n kubectl-read-only:\n kubectl:\n commands:\n resources:\n - deployments\n - pods\n - namespaces\n - daemonsets\n - statefulsets\n - storageclasses\n - nodes\n verbs:\n - api-resources\n - api-versions\n - cluster-info\n - describe\n - diff\n - explain\n - get\n - logs\n - top\n - auth\n defaultNamespace: default\n enabled: true\n namespaces:\n include:\n - .*\n restrictAccess: false\nsettings:\n clusterName: qa\nsources:\n kubernetes-info:\n displayName: Kubernetes Information\n kubernetes:\n recommendations:\n ingress:\n backendServiceValid: true\n tlsSecretValid: true\n pod:\n labelsSet: true\n noLatestImageTag: true\n",
}, nil
}

func TestGqlProviderSuccess(t *testing.T) {
//given
f := fakeGqlClient{}
p := NewGqlProvider(&f)
expected, err := os.ReadFile("testdata/TestGqlProviderSuccess/config.yaml")
require.NoError(t, err)

// when
configs, err := p.Configs(context.Background())
configs, ver, err := p.Configs(context.Background())

// then
assert.NoError(t, err)
content, err := os.ReadFile("testdata/TestGqlProviderSuccess/config.yaml")
assert.NoError(t, err)
assert.Equal(t, configs[0], content)
assert.Equal(t, string(expected), string(configs[0]))
assert.Equal(t, 3, ver)
}
4 changes: 2 additions & 2 deletions internal/config/provider.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package config

import (
"github.com/kubeshop/botkube/pkg/config"
"os"

"github.com/kubeshop/botkube/pkg/config"
)

// GetProvider resolves and returns paths for config files.
Expand All @@ -18,4 +19,3 @@ func GetProvider(remoteCfgSyncEnabled bool, deployClient DeploymentClient) confi

return NewFileSystemProvider(configPathsFlag)
}

69 changes: 69 additions & 0 deletions internal/config/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package config_test

import (
"context"
"os"
"testing"

"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/kubeshop/botkube/internal/config"
)

func TestGetProvider(t *testing.T) {
t.Run("from envs variable only", func(t *testing.T) {
// given
t.Setenv("BOTKUBE_CONFIG_PATHS", "testdata/TestGetProvider/first.yaml,testdata/TestGetProvider/second.yaml,testdata/TestGetProvider/third.yaml")

// when
provider := config.GetProvider(false, nil)
gotConfigs, _, err := provider.Configs(context.Background())
assert.NoError(t, err)

// then
c, err := os.ReadFile("testdata/TestGetProvider/all.yaml")
assert.NoError(t, err)
assert.Equal(t, c, gotConfigs.Merge())
})

t.Run("from CLI flag only", func(t *testing.T) {
// given
fSet := pflag.NewFlagSet("testing", pflag.ContinueOnError)
config.RegisterFlags(fSet)
err := fSet.Parse([]string{"--config=testdata/TestGetProvider/first.yaml,testdata/TestGetProvider/second.yaml", "--config", "testdata/TestGetProvider/third.yaml"})
require.NoError(t, err)

// when
provider := config.GetProvider(false, nil)
gotConfigs, _, err := provider.Configs(context.Background())
assert.NoError(t, err)

// then
c, err := os.ReadFile("testdata/TestGetProvider/all.yaml")
assert.NoError(t, err)
assert.Equal(t, c, gotConfigs.Merge())
})

t.Run("should honor env variable over the CLI flag", func(t *testing.T) {
// given
fSet := pflag.NewFlagSet("testing", pflag.ContinueOnError)
config.RegisterFlags(fSet)

err := fSet.Parse([]string{"--config=testdata/TestGetProvider/from-cli-flag.yaml,testdata/TestGetProvider/from-cli-flag-second.yaml"})
require.NoError(t, err)

t.Setenv("BOTKUBE_CONFIG_PATHS", "testdata/TestGetProvider/first.yaml,testdata/TestGetProvider/second.yaml,testdata/TestGetProvider/third.yaml")

// when
provider := config.GetProvider(false, nil)
gotConfigs, _, err := provider.Configs(context.Background())
assert.NoError(t, err)

// then
c, err := os.ReadFile("testdata/TestGetProvider/all.yaml")
assert.NoError(t, err)
assert.Equal(t, c, gotConfigs.Merge())
})
}
Loading

0 comments on commit 5c3f787

Please sign in to comment.