Skip to content

Commit 3631753

Browse files
committed
Testing create
1 parent 22d03f0 commit 3631753

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

pkg/commands/create_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright (C) 2015 Scaleway. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
package commands
6+
7+
import (
8+
"bytes"
9+
"strings"
10+
"testing"
11+
12+
. "github.com/scaleway/scaleway-cli/vendor/github.com/smartystreets/goconvey/convey"
13+
)
14+
15+
func ExampleRunCreate() {
16+
ctx := ExampleCommandContext()
17+
args := CreateArgs{}
18+
RunCreate(ctx, args)
19+
}
20+
21+
func ExampleRunCreate_complex() {
22+
ctx := ExampleCommandContext()
23+
args := CreateArgs{
24+
Name: "test",
25+
Bootscript: "rescue",
26+
Tags: []string{"tag1", "tag2"},
27+
Volumes: []string{},
28+
Image: "ubuntu-vivid",
29+
TmpSSHKey: false,
30+
}
31+
RunCreate(ctx, args)
32+
}
33+
34+
func TestRunCreate_realAPI(t *testing.T) {
35+
createdUUIDs := []string{}
36+
ctx := RealAPIContext()
37+
if ctx == nil {
38+
t.Skip()
39+
}
40+
41+
// FIXME: test empty settings
42+
// FIXME: test cache duplicates
43+
// FIXME: test TmpSSHKey
44+
// FIXME: test Volumes
45+
// FIXME: test Tags
46+
// FIXME: test Bootscript
47+
48+
Convey("Testing RunCreate() on real API", t, func() {
49+
// Error when image is empty !
50+
/*
51+
Convey("no options", func() {
52+
args := CreateArgs{}
53+
err := RunCreate(*ctx, args)
54+
So(err, ShouldBeNil)
55+
56+
stderr := ctx.Stderr.(*bytes.Buffer).String()
57+
stdout := ctx.Stdout.(*bytes.Buffer).String()
58+
fmt.Println(stderr)
59+
fmt.Println(stdout)
60+
})
61+
*/
62+
63+
Convey("--name=unittest-create-standard ubuntu-vivid", func() {
64+
args := CreateArgs{
65+
Name: "unittest-create-standard",
66+
Image: "ubuntu-vivid",
67+
}
68+
err := RunCreate(*ctx, args)
69+
So(err, ShouldBeNil)
70+
71+
stderr := ctx.Stderr.(*bytes.Buffer).String()
72+
stdout := ctx.Stdout.(*bytes.Buffer).String()
73+
So(stderr, ShouldBeEmpty)
74+
So(stdout, shouldBeAnUUID)
75+
76+
createdUUIDs = append(createdUUIDs, strings.TrimSpace(stdout))
77+
})
78+
79+
Reset(func() {
80+
ctx.Stdout.(*bytes.Buffer).Reset()
81+
ctx.Stderr.(*bytes.Buffer).Reset()
82+
83+
if len(createdUUIDs) > 0 {
84+
err := RunRm(*ctx, RmArgs{
85+
Servers: createdUUIDs,
86+
})
87+
So(err, ShouldBeNil)
88+
89+
stderr := ctx.Stderr.(*bytes.Buffer).String()
90+
stdout := ctx.Stdout.(*bytes.Buffer).String()
91+
So(stderr, ShouldBeEmpty)
92+
removedUUIDs := strings.Split(strings.TrimSpace(stdout), "\n")
93+
So(removedUUIDs, ShouldResemble, createdUUIDs)
94+
95+
createdUUIDs = createdUUIDs[:0]
96+
97+
ctx.Stdout.(*bytes.Buffer).Reset()
98+
ctx.Stderr.(*bytes.Buffer).Reset()
99+
}
100+
})
101+
})
102+
}

pkg/commands/test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/pborman/uuid"
8+
)
9+
10+
func shouldBeAnUUID(actual interface{}, expected ...interface{}) string {
11+
input := actual.(string)
12+
input = strings.TrimSpace(input)
13+
uuid := uuid.Parse(input)
14+
if uuid == nil {
15+
return fmt.Sprintf("%q should be an UUID", actual)
16+
}
17+
return ""
18+
}

0 commit comments

Comments
 (0)