File: sync_test.go

package info (click to toggle)
golang-github-crc-org-crc 2.34.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,548 kB
  • sloc: sh: 398; makefile: 326; javascript: 40
file content (180 lines) | stat: -rw-r--r-- 4,536 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package machine

import (
	"context"
	"errors"
	"sync"
	"testing"

	"github.com/crc-org/crc/v2/pkg/crc/machine/state"
	"github.com/crc-org/crc/v2/pkg/crc/machine/types"
	crcPreset "github.com/crc-org/crc/v2/pkg/crc/preset"
	"github.com/stretchr/testify/assert"
)

func TestOneStartAtTheSameTime(t *testing.T) {
	isRunning := make(chan struct{}, 1)
	startCh := make(chan struct{}, 1)
	waitingMachine := &waitingMachine{
		isRunning:       isRunning,
		startCompleteCh: startCh,
	}
	syncMachine := NewSynchronizedMachine(waitingMachine)
	assert.Equal(t, Idle, syncMachine.CurrentState())

	lock := &sync.WaitGroup{}
	lock.Add(1)
	go func() {
		defer lock.Done()
		_, err := syncMachine.Start(context.Background(), types.StartConfig{})
		assert.NoError(t, err)
	}()

	<-isRunning
	assert.Equal(t, Starting, syncMachine.CurrentState())
	assert.Equal(t, waitingMachine.GetName(), syncMachine.GetName())
	_, err := syncMachine.Start(context.Background(), types.StartConfig{})
	assert.EqualError(t, err, "cluster is busy")

	startCh <- struct{}{}
	lock.Wait()

	assert.Equal(t, Idle, syncMachine.CurrentState())
}

func TestDeleteStop(t *testing.T) {
	isRunning := make(chan struct{}, 1)
	deleteCh := make(chan struct{}, 1)
	waitingMachine := &waitingMachine{
		isRunning:        isRunning,
		deleteCompleteCh: deleteCh,
	}
	syncMachine := NewSynchronizedMachine(waitingMachine)
	assert.Equal(t, Idle, syncMachine.CurrentState())

	lock := &sync.WaitGroup{}
	lock.Add(1)
	go func() {
		defer lock.Done()
		assert.NoError(t, syncMachine.Delete())
	}()

	<-isRunning
	assert.Equal(t, Deleting, syncMachine.CurrentState())
	assert.EqualError(t, syncMachine.Delete(), "cluster is stopping or deleting")
	_, err := syncMachine.Stop()
	assert.EqualError(t, err, "cluster is stopping or deleting")
	_, err = syncMachine.Start(context.Background(), types.StartConfig{})
	assert.EqualError(t, err, "cluster is busy")

	deleteCh <- struct{}{}
	lock.Wait()

	assert.Equal(t, Idle, syncMachine.CurrentState())
}

func TestCancelStart(t *testing.T) {
	isRunning := make(chan struct{}, 1)
	deleteCh := make(chan struct{}, 1)
	waitingMachine := &waitingMachine{
		isRunning:        isRunning,
		startCompleteCh:  make(chan struct{}, 1),
		deleteCompleteCh: deleteCh,
	}
	syncMachine := NewSynchronizedMachine(waitingMachine)
	assert.Equal(t, Idle, syncMachine.CurrentState())

	lock := &sync.WaitGroup{}
	lock.Add(1)
	go func() {
		defer lock.Done()
		_, err := syncMachine.Start(context.Background(), types.StartConfig{})
		assert.EqualError(t, err, "context canceled")
	}()

	<-isRunning
	assert.Equal(t, Starting, syncMachine.CurrentState())

	lock.Add(1)
	go func() {
		defer lock.Done()
		assert.NoError(t, syncMachine.Delete())
	}()

	deleteCh <- struct{}{}
	lock.Wait()

	assert.Equal(t, Idle, syncMachine.CurrentState())
}

type waitingMachine struct {
	isRunning        chan struct{}
	startCompleteCh  chan struct{}
	stopCompleteCh   chan struct{}
	deleteCompleteCh chan struct{}
}

func (m *waitingMachine) IsRunning() (bool, error) {
	return false, errors.New("not implemented")
}

func (m *waitingMachine) GetName() string {
	return "waiting machine"
}

func (m *waitingMachine) Delete() error {
	m.isRunning <- struct{}{}
	<-m.deleteCompleteCh
	return nil
}

func (m *waitingMachine) Exists() (bool, error) {
	return false, errors.New("not implemented")
}

func (m *waitingMachine) GetConsoleURL() (*types.ConsoleResult, error) {
	return nil, errors.New("not implemented")
}

func (m *waitingMachine) ConnectionDetails() (*types.ConnectionDetails, error) {
	return nil, errors.New("not implemented")
}

func (m *waitingMachine) PowerOff() error {
	return nil
}

func (m *waitingMachine) Start(context context.Context, _ types.StartConfig) (*types.StartResult, error) {
	m.isRunning <- struct{}{}
	select {
	case <-context.Done():
		return nil, context.Err()
	case <-m.startCompleteCh:
		return &types.StartResult{
			Status:         state.Running,
			KubeletStarted: true,
		}, nil
	}
}

func (m *waitingMachine) Status() (*types.ClusterStatusResult, error) {
	return nil, errors.New("not implemented")
}

func (m *waitingMachine) Stop() (state.State, error) {
	m.isRunning <- struct{}{}
	<-m.stopCompleteCh
	return state.Stopped, nil
}

func (m *waitingMachine) GenerateBundle(_ bool) error {
	return errors.New("not implemented")
}

func (m *waitingMachine) GetPreset() crcPreset.Preset {
	return crcPreset.OpenShift
}

func (m *waitingMachine) GetClusterLoad() (*types.ClusterLoadResult, error) {
	return nil, errors.New("not implemented")
}