Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Move test utilities to a package for sharing between unit and integra…
Browse files Browse the repository at this point in the history
…tion tests
  • Loading branch information
blakewatters committed Apr 21, 2020
1 parent 111e212 commit eaf2aab
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 52 deletions.
13 changes: 7 additions & 6 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ import (
"os"
"testing"

"github.com/opsani/cli/test"
"github.com/spf13/viper"
"github.com/stretchr/testify/suite"
)

type ConfigTestSuite struct {
suite.Suite
*OpsaniCommandExecutor
*test.OpsaniCommandExecutor
}

func TestConfigTestSuite(t *testing.T) {
suite.Run(t, new(ConfigTestSuite))
}

func (s *ConfigTestSuite) SetupSuite() {
s.OpsaniCommandExecutor = NewOpsaniCommandExecutor(rootCmd)
s.OpsaniCommandExecutor = test.NewOpsaniCommandExecutor(rootCmd)
}

func (s *ConfigTestSuite) SetupTest() {
Expand All @@ -45,7 +46,7 @@ func TestMain(m *testing.M) {
}

func (s *ConfigTestSuite) TestRunningConfigFileDoesntExist() {
configFile := TempConfigFileWithBytes([]byte{})
configFile := test.TempConfigFileWithBytes([]byte{})
os.Remove(configFile.Name())

_, err := s.ExecuteWithConfig(configFile, "config")
Expand All @@ -54,23 +55,23 @@ func (s *ConfigTestSuite) TestRunningConfigFileDoesntExist() {
}

func (s *ConfigTestSuite) TestRunningConfigFileEmpty() {
configFile := TempConfigFileWithBytes([]byte{})
configFile := test.TempConfigFileWithBytes([]byte{})
defer os.Remove(configFile.Name())

_, err := s.ExecuteWithConfig(configFile, "config")
s.Require().EqualError(err, "command failed because client is not initialized. Run \"opsani init\" and try again")
}

func (s *ConfigTestSuite) TestRunningConfigWithInvalidFile() {
configFile := TempConfigFileWithString("malformed:yaml:ysdsfsd")
configFile := test.TempConfigFileWithString("malformed:yaml:ysdsfsd")
defer os.Remove(configFile.Name())
_, err := s.ExecuteWithConfig(configFile, "config")
s.Require().Error(err)
s.Require().EqualError(err, "error parsing configuration file: While parsing config: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `malform...` into map[string]interface {}")
}

func (s *ConfigTestSuite) TestRunningWithInitializedConfig() {
configFile := TempConfigFileWithObj(map[string]interface{}{"app": "example.com/app1", "token": "123456"})
configFile := test.TempConfigFileWithObj(map[string]interface{}{"app": "example.com/app1", "token": "123456"})
defer os.Remove(configFile.Name())
output, err := s.ExecuteWithConfig(configFile, "config")
s.Require().NoError(err)
Expand Down
47 changes: 1 addition & 46 deletions cmd/helpers.go → test/command_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package test

import (
"bytes"
"io/ioutil"
"os"

"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
)

// NewCommandExecutor returns an executor for testing Cobra commands
Expand Down Expand Up @@ -59,45 +56,3 @@ func NewOpsaniCommandExecutor(rootCmd *cobra.Command) *OpsaniCommandExecutor {
CommandExecutor: NewCommandExecutor(rootCmd),
}
}

// OpsaniCommandExecutor provides an interface for executing Opsani CLI commands in tests
type OpsaniCommandExecutor struct {
*CommandExecutor
}

// ExecuteWithConfig runs an Opsani CLI command with the given config file and arguments and returns the output captured
func (oce *OpsaniCommandExecutor) ExecuteWithConfig(configFile *os.File, args ...string) (output string, err error) {
return oce.Execute(append([]string{"--config", configFile.Name()}, args...)...)
}

// ExecuteCWithConfig runs an Opsani CLI command with the given config file and arguments and returns the Opsani CLI command invoked
func (oce *OpsaniCommandExecutor) ExecuteCWithConfig(configFile *os.File, args ...string) (c *cobra.Command, output string, err error) {
return oce.ExecuteC(append([]string{"--config", configFile.Name()}, args...)...)
}

// TempConfigFileWithBytes returns a temporary YAML config file with the given byte array content
func TempConfigFileWithBytes(bytes []byte) *os.File {
tmpFile, err := ioutil.TempFile("", "*.yaml")
if err != nil {
panic("failed to create temp file")
}
if _, err = tmpFile.Write(bytes); err != nil {
panic("failed writing to temp file")
}
return tmpFile
}

// TempConfigFileWithString returns a temporary YAML config file with the given string content
func TempConfigFileWithString(str string) *os.File {
return TempConfigFileWithBytes([]byte(str))
}

// TempConfigFileWithObj returns a temporary YAML config file with the given object serialized to YAML
func TempConfigFileWithObj(obj interface{}) *os.File {
if data, err := yaml.Marshal(obj); data != nil {
return TempConfigFileWithBytes(data)
} else if err != nil {
panic("failed serializing config to YAML")
}
return nil
}
36 changes: 36 additions & 0 deletions test/opsani_command_executor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2020 Opsani
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package test

import (
"os"

"github.com/spf13/cobra"
)

// OpsaniCommandExecutor provides an interface for executing Opsani CLI commands in tests
type OpsaniCommandExecutor struct {
*CommandExecutor
}

// ExecuteWithConfig runs an Opsani CLI command with the given config file and arguments and returns the output captured
func (oce *OpsaniCommandExecutor) ExecuteWithConfig(configFile *os.File, args ...string) (output string, err error) {
return oce.Execute(append([]string{"--config", configFile.Name()}, args...)...)
}

// ExecuteCWithConfig runs an Opsani CLI command with the given config file and arguments and returns the Opsani CLI command invoked
func (oce *OpsaniCommandExecutor) ExecuteCWithConfig(configFile *os.File, args ...string) (c *cobra.Command, output string, err error) {
return oce.ExecuteC(append([]string{"--config", configFile.Name()}, args...)...)
}
49 changes: 49 additions & 0 deletions test/temp_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020 Opsani
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package test

import (
"io/ioutil"
"os"

"sigs.k8s.io/yaml"
)

// TempConfigFileWithBytes returns a temporary YAML config file with the given byte array content
func TempConfigFileWithBytes(bytes []byte) *os.File {
tmpFile, err := ioutil.TempFile("", "*.yaml")
if err != nil {
panic("failed to create temp file")
}
if _, err = tmpFile.Write(bytes); err != nil {
panic("failed writing to temp file")
}
return tmpFile
}

// TempConfigFileWithString returns a temporary YAML config file with the given string content
func TempConfigFileWithString(str string) *os.File {
return TempConfigFileWithBytes([]byte(str))
}

// TempConfigFileWithObj returns a temporary YAML config file with the given object serialized to YAML
func TempConfigFileWithObj(obj interface{}) *os.File {
if data, err := yaml.Marshal(obj); data != nil {
return TempConfigFileWithBytes(data)
} else if err != nil {
panic("failed serializing config to YAML")
}
return nil
}

0 comments on commit eaf2aab

Please sign in to comment.