Bump loofah from 2.2.3 to 2.3.1 in /apps/workbench
[arvados.git] / lib / cloud / cloudtest / tester_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package cloudtest
6
7 import (
8         "bytes"
9         "testing"
10         "time"
11
12         "git.curoverse.com/arvados.git/lib/cloud"
13         "git.curoverse.com/arvados.git/lib/dispatchcloud/test"
14         "git.curoverse.com/arvados.git/sdk/go/arvados"
15         "git.curoverse.com/arvados.git/sdk/go/ctxlog"
16         "golang.org/x/crypto/ssh"
17         check "gopkg.in/check.v1"
18 )
19
20 // Gocheck boilerplate
21 func Test(t *testing.T) {
22         check.TestingT(t)
23 }
24
25 var _ = check.Suite(&TesterSuite{})
26
27 type TesterSuite struct {
28         stubDriver *test.StubDriver
29         cluster    *arvados.Cluster
30         tester     *tester
31         log        bytes.Buffer
32 }
33
34 func (s *TesterSuite) SetUpTest(c *check.C) {
35         pubkey, privkey := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_dispatch")
36         _, privhostkey := test.LoadTestKey(c, "../../dispatchcloud/test/sshkey_vm")
37         s.stubDriver = &test.StubDriver{
38                 HostKey:                   privhostkey,
39                 AuthorizedKeys:            []ssh.PublicKey{pubkey},
40                 ErrorRateDestroy:          0.1,
41                 MinTimeBetweenCreateCalls: time.Millisecond,
42         }
43         tagKeyPrefix := "tagprefix:"
44         s.cluster = &arvados.Cluster{
45                 ManagementToken: "test-management-token",
46                 Containers: arvados.ContainersConfig{
47                         CloudVMs: arvados.CloudVMsConfig{
48                                 SyncInterval:   arvados.Duration(10 * time.Millisecond),
49                                 TimeoutBooting: arvados.Duration(150 * time.Millisecond),
50                                 TimeoutProbe:   arvados.Duration(15 * time.Millisecond),
51                                 ProbeInterval:  arvados.Duration(5 * time.Millisecond),
52                                 ResourceTags:   map[string]string{"testtag": "test value"},
53                         },
54                 },
55                 InstanceTypes: arvados.InstanceTypeMap{
56                         test.InstanceType(1).Name: test.InstanceType(1),
57                         test.InstanceType(2).Name: test.InstanceType(2),
58                         test.InstanceType(3).Name: test.InstanceType(3),
59                 },
60         }
61         s.tester = &tester{
62                 Logger:           ctxlog.New(&s.log, "text", "info"),
63                 Tags:             cloud.SharedResourceTags{"testtagkey": "testtagvalue"},
64                 TagKeyPrefix:     tagKeyPrefix,
65                 SetID:            cloud.InstanceSetID("test-instance-set-id"),
66                 ProbeInterval:    5 * time.Millisecond,
67                 SyncInterval:     10 * time.Millisecond,
68                 TimeoutBooting:   150 * time.Millisecond,
69                 Driver:           s.stubDriver,
70                 DriverParameters: nil,
71                 InstanceType:     test.InstanceType(2),
72                 ImageID:          "test-image-id",
73                 SSHKey:           privkey,
74                 BootProbeCommand: "crunch-run --list",
75                 ShellCommand:     "true",
76         }
77 }
78
79 func (s *TesterSuite) TestSuccess(c *check.C) {
80         s.tester.Logger = ctxlog.TestLogger(c)
81         ok := s.tester.Run()
82         c.Check(ok, check.Equals, true)
83 }
84
85 func (s *TesterSuite) TestBootFail(c *check.C) {
86         s.tester.BootProbeCommand = "falsey"
87         ok := s.tester.Run()
88         c.Check(ok, check.Equals, false)
89         c.Check(s.log.String(), check.Matches, `(?ms).*\\"falsey\\": command not found.*`)
90 }
91
92 func (s *TesterSuite) TestShellCommandFail(c *check.C) {
93         s.tester.ShellCommand = "falsey"
94         ok := s.tester.Run()
95         c.Check(ok, check.Equals, false)
96         c.Check(s.log.String(), check.Matches, `(?ms).*\\"falsey\\": command not found.*`)
97 }