Skip to content

Commit

Permalink
test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed May 3, 2020
1 parent 2e336d1 commit c691bc3
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ matrix:
- go: master

env:
- GO111MODULE=on BEETLE_DATABASE_DRIVER=mysql BEETLE_DATABASE_MYSQL_HOST=127.0.0.1 BEETLE_DATABASE_MYSQL_PORT=3306 BEETLE_DATABASE_MYSQL_DATABASE=beetle BEETLE_DATABASE_MYSQL_USERNAME=root BEETLE_DATABASE_MYSQL_PASSWORD=
- GO111MODULE=on REMOTE_BEETLE_URL=http://127.0.0.1 REMOTE_BEETLE_TOKEN= BEETLE_DATABASE_DRIVER=mysql BEETLE_DATABASE_MYSQL_HOST=127.0.0.1 BEETLE_DATABASE_MYSQL_PORT=3306 BEETLE_DATABASE_MYSQL_DATABASE=beetle BEETLE_DATABASE_MYSQL_USERNAME=root BEETLE_DATABASE_MYSQL_PASSWORD=

install: true

Expand Down
10 changes: 5 additions & 5 deletions internal/app/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var clusterCmd = &cobra.Command{
}

if len(args) > 0 {
err, result = getCluster(args, url, token)
err, result = getCluster(args[0], url, token)
} else {
err, result = getClusters(url, token)
}
Expand All @@ -50,14 +50,14 @@ func init() {
// getClusters Get Clusters List
func getClusters(beetleURL, token string) (error, [][]string) {
return nil, [][]string{
{"Staging", "Down"},
{"Production", "Up"},
{"staging", "down"},
{"production", "up"},
}
}

// getCluster Get Cluster
func getCluster(clusters []string, beetleURL, token string) (error, [][]string) {
func getCluster(cluster string, beetleURL, token string) (error, [][]string) {
return nil, [][]string{
{"Staging", "Down"},
{"staging", "down"},
}
}
83 changes: 83 additions & 0 deletions internal/app/cmd/cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/clivern/beetle/internal/app/module"
"github.com/clivern/beetle/pkg"

"github.com/drone/envsubst"
"github.com/spf13/viper"
)

// TestClusterCMD test cases
func TestClusterCMD(t *testing.T) {
testingConfig := "config.testing.yml"

// LoadConfigFile
t.Run("LoadConfigFile", func(t *testing.T) {
fs := module.FileSystem{}

dir, _ := os.Getwd()
configFile := fmt.Sprintf("%s/%s", dir, testingConfig)

for {
if fs.FileExists(configFile) {
break
}
dir = filepath.Dir(dir)
configFile = fmt.Sprintf("%s/%s", dir, testingConfig)
}

t.Logf("Load Config File %s", configFile)

configUnparsed, _ := ioutil.ReadFile(configFile)
configParsed, _ := envsubst.EvalEnv(string(configUnparsed))
viper.SetConfigType("yaml")
viper.ReadConfig(bytes.NewBuffer([]byte(configParsed)))
})

// TestGetClusters
t.Run("TestGetClusters", func(t *testing.T) {
srv := pkg.MockServer(
"/api/v1/cluster",
`{"clusters": [{"name": "staging", "health": false},{"name": "production", "health": true}]}`,
)

defer srv.Close()

err, result := getClusters(srv.URL, "")

pkg.Expect(t, nil, err)
pkg.Expect(t, result, [][]string{
{"staging", "down"},
{"production", "up"},
})
})

// TestGetCluster
t.Run("TestGetCluster", func(t *testing.T) {
srv := pkg.MockServer(
"/api/v1/cluster/staging",
`{"name": "staging", "health": false}`,
)

defer srv.Close()

err, result := getCluster("staging", srv.URL, "")

pkg.Expect(t, nil, err)
pkg.Expect(t, result, [][]string{
{"staging", "down"},
})
})
}
22 changes: 22 additions & 0 deletions pkg/server_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package pkg

import (
"net/http"
"net/http/httptest"
)

// MockServer mocks http server
func MockServer(uri, response string) *httptest.Server {
handler := http.NewServeMux()
handler.HandleFunc(uri, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(response))
})

srv := httptest.NewServer(handler)

return srv
}

0 comments on commit c691bc3

Please sign in to comment.