Hi I am new to Go and I am writing a simple app which gets some configuration from the env variables. I do this in the init function as shown below.
type envVars struct {
Host string `env:"APP_HOST"`
Username string `env:"APP_USERNAME"`
Password string `env:"APP_PASSWORD"`
}
var envConfig envVars
func init() {
if err := env.Parse(&envConfig); err != nil {
log.Fatal(err)
}
}
I wrote test to verify of the env variables are being read correctly. But the problem is that my program's init func gets called even before my test's init func. Is there any way I can do some sort of setup before my program's init func gets called.
func init() {
os.Setenv("APP_HOST", "http://localhost:9999")
os.Setenv("APP_USERNAME", "john")
os.Setenv("APP_PASSWORD", "doe")
}
func TestEnvConfig(t *testing.T) {
assert.NotNil(t, envConfig)
assert.Equal(t, "http://localhost:9999", envConfig.Host)
}
You can use the TestMain func to control what happens before and after your tests.
For example:
func TestMain(m *testing.M) {
// Write code here to run before tests
// Run tests
exitVal := m.Run()
// Write code here to run after tests
// Exit with exit value from tests
os.Exit(exitVal)
}
func TestYourFunc(t *testing.T) {
// Test code
}
You can add a Test_parse_params(t *testing.T) function before your real tests. Look like this:
type envVars struct {
Host string `env:"APP_HOST"`
Username string `env:"APP_USERNAME"`
Password string `env:"APP_PASSWORD"`
}
var envConfig envVars
//parse command params
func Test_parse_params(t *testing.T) {
if err := env.Parse(&envConfig); err != nil {
log.Fatal(err)
}
}
func Test_real_test(t *testing.T) {
....
}
No, you shouldn't expect init() run in some order, (in fact it based on file loaded order, but still, you should not count on it).
The simple way is, if you want to test it, use a shell script to run you test, or something like Makefile.
Shell example:
set +e
export APP_HOST=http://localhost:9999
export APP_USERNAME=john
export APP_PASSWORD=doe
go test .
unset APP_HOST
unset APP_USERNAME
unset APP_PASSWORD
or a single line command:
APP_HOST=http://localhost:9999 APP_USERNAME=john APP_PASSWORD=doe go test .
Edit:
Other solution: move out the read env from init func.
func init(){
envInit()
}
func envInit(){
if err := env.Parse(&envConfig); err != nil {
log.Fatal(err)
}
}
Then you can call again envInit in your test to make sure it works.
Less than ideal, but this works for me.
Inside of the package that you're testing:
func init() {
if len(os.Args) > 1 && os.Args[1][:5] == "-test" {
log.Println("testing")//special test setup goes goes here
return // ...or just skip the setup entirely
}
//...
}
Related
So I have a web app running in serverless. It spins up a bunch of lambdas and then a 'test' lambda is invoked at a later stage of our pipeline to run some api tests against the other lambbas. It passes or fails code pipeline depending on the result of the tests.
My concern is the implementation of the tests portion itself.
We're using golang, and I wasn't able to successfully find a way to have a bunch of go test files and run them, record the results and determine pass/fail or not.. but I wanted to use the test suite and library in order to run my assertions. So I came up with this solution which was to break about the go test library and run it myself using MainStart(), ex:
// T is used to manage our test cases manually using MainStart
type T struct{}
func (*T) ImportPath() string { return "" }
func (*T) MatchString(pat, str string) (bool, error) { return true, nil }
func (*T) SetPanicOnExit0(bool) {}
func (*T) StartCPUProfile(io.Writer) error { return nil }
func (*T) StopCPUProfile() {}
func (*T) StartTestLog(io.Writer) {}
func (*T) StopTestLog() error { return nil }
func (*T) WriteHeapProfile(io.Writer) error { return nil }
func (*T) WriteProfileTo(string, io.Writer, int) error { return nil }
func (h *Handler) Handle(ctx context.Context, event events.CodePipelineEvent) (interface{}, error) {
job := event.CodePipelineJob
ok := runTest()
if ok {
logger.Info("Tests Passed!")
input := &codepipeline.PutJobSuccessResultInput{
JobId: &job.ID,
success, err := h.service.PutJobSuccessResult(input)
return success, err
} else {
logger.Info("Tests Failed :(")
input := &codepipeline.PutJobFailureResultInput{
JobId: &job.ID,
FailureDetails: &codepipeline.FailureDetails{
Message: aws.String("tests failed"),
Type: aws.String("JobFailed"),
},
failure, err := h.service.PutJobFailureResult(input)
return failure, err
}
}
}
// NewHandler returns a pointer to a Handler struct
func NewHandler(service *codepipeline.CodePipeline) *Handler {
return &Handler{
service,
}
}
// runTest returns the test results
func runTest() bool {
testSuite := []testing.InternalTest{
{Name: "Test Service", F: TestService},
}
logger.Info("Running Tests")
errors := testing.MainStart(&T{}, testSuite, nil, nil).Run()
if errors == 0 {
return true
} else {
return false
}
}
So the test looks like this:
func TestServices(t *testing.T) {
logger.Info("blah blah do tests")
}
And for reference, main.go contains:
// Initial code pipeline session
cpSession := session.Must(session.NewSession())
// Create the credentials from AssumeRoleProvider to assume the role
// referenced by the "putJobResultRoleArn" ARN.
creds := stscreds.NewCredentials(cpSession, "putJobResultRoleArn", func(p *stscreds.AssumeRoleProvider) {
p.RoleARN = roleArn
p.RoleSessionName = "put_job_result_session"
})
// Create a new instance of the CodePipeline client with a session
svc := codepipeline.New(cpSession, &aws.Config{Credentials: creds})
intTesthandler := inttest.NewHandler(
svc,
)
lambda.Start(func(ctx context.Context, event events.CodePipelineEvent) (interface{}, error) {
return intTesthandler.Handle(ctx, event)
})
This works quite well, but what I don't like is:
It's using an unsupported approach from golang that could break in the future
I have to handhold any failures, meaning if one fails, I need to write code to stop the rest of the suite (if there were more tests) from running.
Bit of a learning curve
To note the tests I have mainly do an http request to an api endpoint and validate the response + parse/store the data.
Anyone have any other creative solution? Is there a repo out there that makes it simple to run integration tests in golang?
Cheers
In main.go, I have some code that makes a network call to AWS Secrets manager.
func main() {
secretName := os.Getenv("DYNAMO_SECRET")
credentials, err := getSecret(secretName)
if err != nil {
logger.Errorf("Failed to retrieve secret from AWS Secrets manager %+v\n", err)
panic(err)
}
router, err := setupRouter(credentials)
The getSecret(secretName) function makes a network call to AWS Secrets manager underneath the hood. In my unit test for main, I have the code below.
func TestMainProgram(t *testing.T) {
defer mockStartServer(nil)()
defer mockSetupRouter(mux.NewRouter(), nil)()
main()
t.Log("Everything is perfect.")
}
When running my unit test, I want to mock the network call to AWS Secrets Manager. Is it possible to mock the return value of getSecret(secretName)? In a Java context, I'm trying to do something similar using Mockito and the when(functionIsCalled).thenReturn(mockValue) syntax.
You can use a function variable to set it to some other value for testing:
func defaultGetSecret(secretName string) (Credentials, error) {...}
var getSecret=defaultGetSecret
func main() {
...
credentials, err:=getSecret(...)
}
In your tests, you can change getSecret to point to something else:
func TestMain(t *testing.T) {
getSecret=func(secretName string) (Credentials,error) {
return mockCredentials,nil
}
main()
}
I'm writing a kubectl plugin to authenticate users, and I would like to prompt the user for a password after the plugin is invoked. From what I understand, it's fairly trivial to get input from STDIN, but I'm struggling seeing messages written to STDOUT. Currently my code looks like this:
In cmd/kubectl-myauth.go:
// This is mostly boilerplate, but it's needed for the MRE
// https://stackoverflow.com/help/minimal-reproducible-example
package myauth
import (...)
func main() {
pflag.CommandLine = pflag.NewFlagSet("kubectl-myauth", pflag.ExitOnError)
root := cmd.NewCmdAuthOp(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})
if err := root.Execute(); err != nil {
os.Exit(1)
}
}
In pkg/cmd/auth.go:
package cmd
...
type AuthOpOptions struct {
configFlags *genericclioptions.ConfigFlags
resultingContext *api.Context
rawConfig api.Config
args []string
...
genericclioptions.IOStreams
}
func NewAuthOpOptions(streams genericclioptions.IOStreams) *AuthOpOptions {
return &AuthOpOptions{
configFlags: genericclioptions.NewConfigFlags(true),
IOStreams: streams,
}
}
func NewCmdAuthOp(streams genericclioptions.IOStreams) *cobra.Command {
o := NewAuthOpOptions(streams)
cmd := &cobra.Command{
RunE: func(c *cobra.Command, args []string) error {
return o.Run()
},
}
return cmd
}
func (o *AuthOpOptions) Run() error {
pass, err := getPassword(o)
if err != nil {
return err
}
// Do Auth Stuff
// Eventually print an ExecCredential to STDOUT
return nil
}
func getPassword(o *AuthOpOptions) (string, error) {
var reader *bufio.Reader
reader = nil
pass := ""
for pass == "" {
// THIS IS AN IMPORTANT LINE [1]
fmt.Fprintf(o.IOStreams.Out, "Password with which to authenticate:\n")
// THE REST OF THIS IS STILL IMPORTANT, BUT LESS SO [2]
if reader == nil {
// The first time through, initialize the reader
reader = bufio.NewReader(o.IOStreams.In)
}
pass, err := reader.ReadString('\n')
if err != nil {
return nil, err
}
pass = strings.Trim(pass, "\r\n")
if pass == "" {
// ALSO THIS LINE IS IMPORTANT [3]
fmt.Fprintf(o.IOStreams.Out, `Read password was empty string.
Please input a valid password.
`)
}
}
return pass, nil
}
This works the way that I expect when running from outside of the kubectl context - namely, it prints the string, prompts for input, and continues. However, from inside the kubectl context, I believe the print between the first two all-caps comments ([1] and [2]) is being swallowed by kubectl listening on STDOUT. I can get around this by printing to STDERR, but that feels... wrong. Is there a way that I can bypass kubectl's consumption of STDOUT to communicate with the user?
TL;DR: kubectl appears to be swallowing all of STDOUT for kubectl plugins, but I want to prompt the user for input - is there a simple way to do this?
Sorry I have no better answer than "Works for me" :-) Here are the steps:
git clone https://github.com/kubernetes/kubernetes.git
duplicate sample-cli-plugin as test-cli-plugin (this involves fixing import-restrictions.yaml, rules-godeps.yaml and rules.yaml under staging/publishing - maybe not necessary, but it's safer this way)
change kubectl-ns.go to kubectl-test.go:
package main
import (
"os"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/test-cli-plugin/pkg/cmd"
)
func main() {
flags := pflag.NewFlagSet("kubectl-test", pflag.ExitOnError)
pflag.CommandLine = flags
root := cmd.NewCmdTest(genericclioptions.IOStreams{In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr})
if err := root.Execute(); err != nil {
os.Exit(1)
}
}
change ns.go to test.go:
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
type TestOptions struct {
configFlags *genericclioptions.ConfigFlags
genericclioptions.IOStreams
}
func NewTestOptions(streams genericclioptions.IOStreams) *TestOptions {
return &TestOptions{
configFlags: genericclioptions.NewConfigFlags(true),
IOStreams: streams,
}
}
func NewCmdTest(streams genericclioptions.IOStreams) *cobra.Command {
o := NewTestOptions(streams)
cmd := &cobra.Command{
Use: "test",
Short: "Test plugin",
SilenceUsage: true,
RunE: func(c *cobra.Command, args []string) error {
o.Run()
return nil
},
}
return cmd
}
func (o *TestOptions) Run() error {
fmt.Fprintf(os.Stderr, "Testing Fprintf Stderr\n")
fmt.Fprintf(os.Stdout, "Testing Fprintf Stdout\n")
fmt.Printf("Testing Printf\n")
fmt.Fprintf(o.IOStreams.Out, "Testing Fprintf o.IOStreams.Out\n")
return nil
}
fix BUILD files accordingly
build the plugin
run make
copy kubectl-test to /usr/local/bin
run the compiled kubectl binary:
~/k8s/_output/bin$ ./kubectl test
Testing Fprintf Stderr
Testing Fprintf Stdout
Testing Printf
Testing Fprintf o.IOStreams.Out
My tests keep failing with but no actual calls happened but I am positive the func is getting called (It's a logging function so I see the logs on the terminal)
Basically I have code that looks something like this :
common/utils.go
func LogNilValue(ctx string){
log.Logger.Warn(ctx)
}
main.go
import (
"common/utils"
)
func CheckFunc(*string value) {
ctx := "Some context string"
if value == nil {
utils.LogNilValue(ctx) //void func that just logs the string
}
}
test.go
type MyMockedObject struct{
mock.Mock
}
func TestNil() {
m := new(MyMockedObject)
m.Mock.On("LogNilValue", mock.Anything).Return(nil)
CheckFunc(nil)
m.AssertCalled(s.T(), "LogNilValue", mock.Anything)
}
I expect this to work but then, I keep getting no actual calls happened. Not sure what I am doing wrong here.
LogNilValue should have MyMockedObject as the method receiver, in order to mock the method. Something like this
func (m *MyMockedObject)LogNilValue(ctx string) {
args := m.Called(ctx)
}
CheckFunc should look like this:
func CheckFunc(value *string, m *MyMockedObject) {
ctx := "Some context string"
if value == nil {
m.LogNilValue(ctx) //void func that just logs the string
}
}
And finally the TestNil method:
func TestNil() {
m := new(MyMockedObject)
m.Mock.On("LogNilValue", mock.Anything).Return(nil)
CheckFunc(nil, m)
m.AssertCalled(s.T(), "LogNilValue", mock.Anything)
}
I have a problem firing a function declared as variable in golang with testify.
Test and function both declared in same package.
var testableFunction = func(abc string) string {...}
now i have a different file with unit test calling testableFunction
func TestFunction(t *testing.T){
...
res:=testableFunction("abc")
...
}
Calling TestFunction with go test does not fire any exception, but testableFunction is actually never run. Why?
That's because your testableFunction variable gets assigned somewhere else in your code.
See this example:
var testableFunction = func(s string) string {
return "re: " + s
}
Test code:
func TestFunction(t *testing.T) {
exp := "re: a"
if got := testableFunction("a"); got != exp {
t.Errorf("Expected: %q, got: %q", exp, got)
}
}
Running go test -cover:
PASS
coverage: 100.0% of statements
ok play 0.002s
Obviously if a new function value is assigned to testableFunction before the test execution, then the anonymous function used to initialize your variable will not get called by the test.
To demonstrate, change your test function to this:
func TestFunction(t *testing.T) {
testableFunction = func(s string) string { return "re: " + s }
exp := "re: a"
if got := testableFunction("a"); got != exp {
t.Errorf("Expected: %q, got: %q", exp, got)
}
}
Running go test -cover:
PASS
coverage: 0.0% of statements
ok play 0.003s