|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package runner |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + |
| 16 | + "github.com/elastic/elastic-agent/pkg/testing/define" |
| 17 | +) |
| 18 | + |
| 19 | +func TestNewRunner_Clean(t *testing.T) { |
| 20 | + tmpdir := t.TempDir() |
| 21 | + stateDir := filepath.Join(tmpdir, "state") |
| 22 | + err := os.MkdirAll(stateDir, 0755) |
| 23 | + require.NoError(t, err) |
| 24 | + |
| 25 | + cfg := Config{ |
| 26 | + AgentVersion: "8.10.0", |
| 27 | + AgentStackVersion: "8.10.0-SNAPSHOT", |
| 28 | + BuildDir: filepath.Join(tmpdir, "build"), |
| 29 | + GOVersion: "1.20.7", |
| 30 | + RepoDir: filepath.Join(tmpdir, "repo"), |
| 31 | + StateDir: stateDir, |
| 32 | + ExtraEnv: nil, |
| 33 | + } |
| 34 | + ip := &fakeInstanceProvisioner{} |
| 35 | + sp := &fakeStackProvisioner{} |
| 36 | + r, err := NewRunner(cfg, ip, sp) |
| 37 | + require.NoError(t, err) |
| 38 | + |
| 39 | + i1 := Instance{ |
| 40 | + ID: "id-1", |
| 41 | + Name: "name-1", |
| 42 | + IP: "127.0.0.1", |
| 43 | + Username: "ubuntu", |
| 44 | + RemotePath: "/home/ubuntu/agent", |
| 45 | + Internal: map[string]interface{}{}, // ElementsMatch fails without this set |
| 46 | + } |
| 47 | + err = r.addOrUpdateInstance(StateInstance{ |
| 48 | + Instance: i1, |
| 49 | + Prepared: true, |
| 50 | + }) |
| 51 | + require.NoError(t, err) |
| 52 | + i2 := Instance{ |
| 53 | + ID: "id-2", |
| 54 | + Name: "name-2", |
| 55 | + IP: "127.0.0.2", |
| 56 | + Username: "ubuntu", |
| 57 | + RemotePath: "/home/ubuntu/agent", |
| 58 | + Internal: map[string]interface{}{}, // ElementsMatch fails without this set |
| 59 | + } |
| 60 | + err = r.addOrUpdateInstance(StateInstance{ |
| 61 | + Instance: i2, |
| 62 | + Prepared: true, |
| 63 | + }) |
| 64 | + require.NoError(t, err) |
| 65 | + s1 := Stack{ |
| 66 | + ID: "id-1", |
| 67 | + Version: "8.10.0", |
| 68 | + Internal: map[string]interface{}{}, // ElementsMatch fails without this set |
| 69 | + } |
| 70 | + err = r.addOrUpdateStack(s1) |
| 71 | + require.NoError(t, err) |
| 72 | + s2 := Stack{ |
| 73 | + ID: "id-2", |
| 74 | + Version: "8.9.0", |
| 75 | + Internal: map[string]interface{}{}, // ElementsMatch fails without this set |
| 76 | + } |
| 77 | + err = r.addOrUpdateStack(s2) |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + // create the runner again ensuring that it loads the saved state |
| 81 | + r, err = NewRunner(cfg, ip, sp) |
| 82 | + require.NoError(t, err) |
| 83 | + |
| 84 | + // clean should use the stored state |
| 85 | + err = r.Clean() |
| 86 | + require.NoError(t, err) |
| 87 | + |
| 88 | + assert.ElementsMatch(t, ip.instances, []Instance{i1, i2}) |
| 89 | + assert.ElementsMatch(t, sp.stacks, []Stack{s1, s2}) |
| 90 | +} |
| 91 | + |
| 92 | +type fakeInstanceProvisioner struct { |
| 93 | + batches []OSBatch |
| 94 | + instances []Instance |
| 95 | +} |
| 96 | + |
| 97 | +func (f *fakeInstanceProvisioner) SetLogger(_ Logger) { |
| 98 | +} |
| 99 | + |
| 100 | +func (f *fakeInstanceProvisioner) Supported(_ define.OS) bool { |
| 101 | + return true |
| 102 | +} |
| 103 | + |
| 104 | +func (f *fakeInstanceProvisioner) Provision(_ context.Context, _ Config, batches []OSBatch) ([]Instance, error) { |
| 105 | + f.batches = batches |
| 106 | + var instances []Instance |
| 107 | + for _, batch := range batches { |
| 108 | + instances = append(instances, Instance{ |
| 109 | + ID: batch.ID, |
| 110 | + Name: batch.ID, |
| 111 | + IP: "127.0.0.1", |
| 112 | + Username: "ubuntu", |
| 113 | + RemotePath: "/home/ubuntu/agent", |
| 114 | + Internal: nil, |
| 115 | + }) |
| 116 | + } |
| 117 | + return instances, nil |
| 118 | +} |
| 119 | + |
| 120 | +func (f *fakeInstanceProvisioner) Clean(_ context.Context, _ Config, instances []Instance) error { |
| 121 | + f.instances = instances |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +type fakeStackProvisioner struct { |
| 126 | + requests []StackRequest |
| 127 | + stacks []Stack |
| 128 | +} |
| 129 | + |
| 130 | +func (f *fakeStackProvisioner) SetLogger(_ Logger) { |
| 131 | +} |
| 132 | + |
| 133 | +func (f *fakeStackProvisioner) Provision(_ context.Context, requests []StackRequest) ([]Stack, error) { |
| 134 | + f.requests = requests |
| 135 | + var stacks []Stack |
| 136 | + for _, req := range requests { |
| 137 | + stacks = append(stacks, Stack{ |
| 138 | + ID: req.ID, |
| 139 | + Version: req.Version, |
| 140 | + Elasticsearch: "http://localhost:9200", |
| 141 | + Kibana: "http://localhost:5601", |
| 142 | + Username: "elastic", |
| 143 | + Password: "changeme", |
| 144 | + Internal: nil, |
| 145 | + }) |
| 146 | + } |
| 147 | + return stacks, nil |
| 148 | +} |
| 149 | + |
| 150 | +func (f *fakeStackProvisioner) Clean(_ context.Context, stacks []Stack) error { |
| 151 | + f.stacks = stacks |
| 152 | + return nil |
| 153 | +} |
0 commit comments