From 4b29f82bb99d325fac278c103c788e97f0644ee1 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Fri, 7 Aug 2015 15:00:41 +0200 Subject: [PATCH 1/2] Testing commands.CommandContext --- pkg/commands/command_test.go | 39 ++++++++++++++++++++++++++++++++++++ pkg/commands/version_test.go | 19 ------------------ 2 files changed, 39 insertions(+), 19 deletions(-) create mode 100644 pkg/commands/command_test.go diff --git a/pkg/commands/command_test.go b/pkg/commands/command_test.go new file mode 100644 index 0000000000..f218557aa6 --- /dev/null +++ b/pkg/commands/command_test.go @@ -0,0 +1,39 @@ +// Copyright (C) 2015 Scaleway. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE.md file. + +// Golang structs for scw commands +package commands + +import ( + "os" + "testing" + + "github.com/scaleway/scaleway-cli/pkg/api" + "github.com/scaleway/scaleway-cli/vendor/github.com/stretchr/testify/assert" +) + +func ExampleCommandContext() CommandContext { + apiClient, err := api.NewScalewayAPI("https://example.org/", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") + if err != nil { + panic(err) + } + + ctx := CommandContext{ + Stdin: os.Stdin, + Stdout: os.Stdout, + Stderr: os.Stderr, + Env: []string{ + "HOME" + os.Getenv("HOME"), + }, + RawArgs: []string{}, + API: apiClient, + } + return ctx +} + +func TestCommandContext_Getenv(t *testing.T) { + ctx := ExampleCommandContext() + assert.Equal(t, ctx.Getenv("HOME"), os.Getenv("HOME")) + assert.Equal(t, ctx.Getenv("DONTEXISTS"), "") +} diff --git a/pkg/commands/version_test.go b/pkg/commands/version_test.go index 57bb38ddad..864ac3ffc1 100644 --- a/pkg/commands/version_test.go +++ b/pkg/commands/version_test.go @@ -6,30 +6,11 @@ package commands import ( "bytes" - "os" "testing" - "github.com/scaleway/scaleway-cli/pkg/api" "github.com/scaleway/scaleway-cli/vendor/github.com/stretchr/testify/assert" ) -func ExampleCommandContext() CommandContext { - apiClient, err := api.NewScalewayAPI("https://example.org/", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") - if err != nil { - panic(err) - } - - ctx := CommandContext{ - Stdin: os.Stdin, - Stdout: os.Stdout, - Stderr: os.Stderr, - Env: []string{}, - RawArgs: []string{}, - API: apiClient, - } - return ctx -} - func TestToto(t *testing.T) { ctx := ExampleCommandContext() var buf bytes.Buffer From e9b5b2a7fae8065177289f28fa6c7a49b34e745e Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Fri, 7 Aug 2015 15:07:57 +0200 Subject: [PATCH 2/2] Added commands.Run examples --- pkg/cli/run.go | 2 +- pkg/commands/run.go | 6 +++--- pkg/commands/run_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 pkg/commands/run_test.go diff --git a/pkg/cli/run.go b/pkg/cli/run.go index 2e1f382d05..5fb191020d 100644 --- a/pkg/cli/run.go +++ b/pkg/cli/run.go @@ -82,7 +82,7 @@ func runRun(cmd *Command, rawArgs []string) { // FIXME: Timeout } ctx := cmd.GetContext(rawArgs) - err := commands.RunRun(ctx, args) + err := commands.Run(ctx, args) if err != nil { logrus.Fatalf("Cannot execute 'run': %v", err) } diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 86629a6161..efe4a4a657 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -13,7 +13,7 @@ import ( "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus" ) -// RunArgs are flags for the `RunRun` function +// RunArgs are flags for the `Run` function type RunArgs struct { Attach bool Bootscript string @@ -28,8 +28,8 @@ type RunArgs struct { // Timeout } -// RunRun is the handler for 'scw run' -func RunRun(ctx CommandContext, args RunArgs) error { +// Run is the handler for 'scw run' +func Run(ctx CommandContext, args RunArgs) error { if args.Gateway == "" { args.Gateway = ctx.Getenv("SCW_GATEWAY") } diff --git a/pkg/commands/run_test.go b/pkg/commands/run_test.go new file mode 100644 index 0000000000..88c56cde8e --- /dev/null +++ b/pkg/commands/run_test.go @@ -0,0 +1,29 @@ +// Copyright (C) 2015 Scaleway. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE.md file. + +package commands + +func ExampleRun() { + ctx := ExampleCommandContext() + args := RunArgs{ + Image: "ubuntu-trusuty", + } + Run(ctx, args) +} + +func ExampleRun_complex() { + ctx := ExampleCommandContext() + args := RunArgs{ + Attach: false, + Bootscript: "rescue", + Command: []string{"ls", "-la"}, + Detach: false, + Gateway: "my-gateway", + Image: "ubuntu-trusty", + Name: "my-test-server", + Tags: []string{"testing", "fake"}, + Volumes: []string{"50G", "1G"}, + } + Run(ctx, args) +}