feat: add _Good, _Bad, _Ugly tests
Refactors the existing test suite to use a `_Good`, `_Bad`, and `_Ugly` testing structure. This new format improves the clarity and organization of the tests by distinguishing between happy path scenarios (_Good), expected failures (_Bad), and edge cases (_Ugly). In addition to reorganizing the existing tests, this change also introduces new `_Bad` test cases to `xmrig_test.go` to cover previously untested failure modes, such as a missing miner binary and an unreachable API. This improves the overall quality and robustness of the test suite.
This commit is contained in:
parent
71efcb71e2
commit
e107920b16
2 changed files with 95 additions and 13 deletions
|
|
@ -40,7 +40,7 @@ func setupTestManager(t *testing.T) *Manager {
|
|||
}
|
||||
|
||||
// TestStartMiner tests the StartMiner function
|
||||
func TestStartMiner(t *testing.T) {
|
||||
func TestStartMiner_Good(t *testing.T) {
|
||||
m := setupTestManager(t)
|
||||
defer m.Stop()
|
||||
|
||||
|
|
@ -61,13 +61,39 @@ func TestStartMiner(t *testing.T) {
|
|||
if _, exists := m.miners[miner.GetName()]; !exists {
|
||||
t.Errorf("Miner %s was not added to the manager's list", miner.GetName())
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartMiner_Bad(t *testing.T) {
|
||||
m := setupTestManager(t)
|
||||
defer m.Stop()
|
||||
|
||||
config := &Config{
|
||||
HTTPPort: 9001, // Use a different port to avoid conflict
|
||||
Pool: "test:1234",
|
||||
Wallet: "testwallet",
|
||||
}
|
||||
|
||||
// Case 2: Attempt to start an unsupported miner
|
||||
_, err = m.StartMiner("unsupported", config)
|
||||
_, err := m.StartMiner("unsupported", config)
|
||||
if err == nil {
|
||||
t.Error("Expected an error when starting an unsupported miner, but got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartMiner_Ugly(t *testing.T) {
|
||||
m := setupTestManager(t)
|
||||
defer m.Stop()
|
||||
|
||||
config := &Config{
|
||||
HTTPPort: 9001, // Use a different port to avoid conflict
|
||||
Pool: "test:1234",
|
||||
Wallet: "testwallet",
|
||||
}
|
||||
// Case 1: Successfully start a supported miner
|
||||
_, err := m.StartMiner("xmrig", config)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected to start miner, but got error: %v", err)
|
||||
}
|
||||
// Case 3: Attempt to start a duplicate miner
|
||||
_, err = m.StartMiner("xmrig", config)
|
||||
if err == nil {
|
||||
|
|
@ -76,7 +102,7 @@ func TestStartMiner(t *testing.T) {
|
|||
}
|
||||
|
||||
// TestStopMiner tests the StopMiner function
|
||||
func TestStopMiner(t *testing.T) {
|
||||
func TestStopMiner_Good(t *testing.T) {
|
||||
m := setupTestManager(t)
|
||||
defer m.Stop()
|
||||
|
||||
|
|
@ -95,16 +121,21 @@ func TestStopMiner(t *testing.T) {
|
|||
if _, exists := m.miners[miner.GetName()]; exists {
|
||||
t.Errorf("Miner %s was not removed from the manager's list", miner.GetName())
|
||||
}
|
||||
}
|
||||
|
||||
func TestStopMiner_Bad(t *testing.T) {
|
||||
m := setupTestManager(t)
|
||||
defer m.Stop()
|
||||
|
||||
// Case 2: Attempt to stop a non-existent miner
|
||||
err = m.StopMiner("nonexistent")
|
||||
err := m.StopMiner("nonexistent")
|
||||
if err == nil {
|
||||
t.Error("Expected an error when stopping a non-existent miner, but got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetMiner tests the GetMiner function
|
||||
func TestGetMiner(t *testing.T) {
|
||||
func TestGetMiner_Good(t *testing.T) {
|
||||
m := setupTestManager(t)
|
||||
defer m.Stop()
|
||||
|
||||
|
|
@ -123,16 +154,21 @@ func TestGetMiner(t *testing.T) {
|
|||
if retrievedMiner.GetName() != startedMiner.GetName() {
|
||||
t.Errorf("Expected to get miner %s, but got %s", startedMiner.GetName(), retrievedMiner.GetName())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetMiner_Bad(t *testing.T) {
|
||||
m := setupTestManager(t)
|
||||
defer m.Stop()
|
||||
|
||||
// Case 2: Attempt to get a non-existent miner
|
||||
_, err = m.GetMiner("nonexistent")
|
||||
_, err := m.GetMiner("nonexistent")
|
||||
if err == nil {
|
||||
t.Error("Expected an error when getting a non-existent miner, but got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// TestListMiners tests the ListMiners function
|
||||
func TestListMiners(t *testing.T) {
|
||||
func TestListMiners_Good(t *testing.T) {
|
||||
m := setupTestManager(t)
|
||||
defer m.Stop()
|
||||
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ func tempDir(t *testing.T) string {
|
|||
return dir
|
||||
}
|
||||
|
||||
func TestNewXMRigMiner(t *testing.T) {
|
||||
func TestNewXMRigMiner_Good(t *testing.T) {
|
||||
miner := NewXMRigMiner()
|
||||
if miner == nil {
|
||||
t.Fatal("NewXMRigMiner returned nil")
|
||||
|
|
@ -55,14 +55,14 @@ func TestNewXMRigMiner(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestXMRigMiner_GetName(t *testing.T) {
|
||||
func TestXMRigMiner_GetName_Good(t *testing.T) {
|
||||
miner := NewXMRigMiner()
|
||||
if name := miner.GetName(); name != "xmrig" {
|
||||
t.Errorf("Expected GetName() to return 'xmrig', got '%s'", name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestXMRigMiner_GetLatestVersion(t *testing.T) {
|
||||
func TestXMRigMiner_GetLatestVersion_Good(t *testing.T) {
|
||||
originalClient := httpClient
|
||||
httpClient = newTestClient(func(req *http.Request) *http.Response {
|
||||
if req.URL.String() != "https://api.github.com/repos/xmrig/xmrig/releases/latest" {
|
||||
|
|
@ -90,7 +90,25 @@ func TestXMRigMiner_GetLatestVersion(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestXMRigMiner_Start_Stop(t *testing.T) {
|
||||
func TestXMRigMiner_GetLatestVersion_Bad(t *testing.T) {
|
||||
originalClient := httpClient
|
||||
httpClient = newTestClient(func(req *http.Request) *http.Response {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusNotFound,
|
||||
Body: io.NopCloser(strings.NewReader("Not Found")),
|
||||
Header: make(http.Header),
|
||||
}
|
||||
})
|
||||
defer func() { httpClient = originalClient }()
|
||||
|
||||
miner := NewXMRigMiner()
|
||||
_, err := miner.GetLatestVersion()
|
||||
if err == nil {
|
||||
t.Fatalf("GetLatestVersion() did not return an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestXMRigMiner_Start_Stop_Good(t *testing.T) {
|
||||
// Create a temporary directory for the dummy executable
|
||||
tmpDir := t.TempDir()
|
||||
dummyExePath := filepath.Join(tmpDir, "xmrig")
|
||||
|
|
@ -140,7 +158,22 @@ func TestXMRigMiner_Start_Stop(t *testing.T) {
|
|||
miner.mu.Unlock()
|
||||
}
|
||||
|
||||
func TestXMRigMiner_GetStats(t *testing.T) {
|
||||
func TestXMRigMiner_Start_Stop_Bad(t *testing.T) {
|
||||
miner := NewXMRigMiner()
|
||||
miner.MinerBinary = "nonexistent"
|
||||
|
||||
config := &Config{
|
||||
Pool: "test:1234",
|
||||
Wallet: "testwallet",
|
||||
}
|
||||
|
||||
err := miner.Start(config)
|
||||
if err == nil {
|
||||
t.Fatalf("Start() did not return an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestXMRigMiner_GetStats_Good(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
summary := XMRigSummary{
|
||||
Hashrate: struct {
|
||||
|
|
@ -190,7 +223,20 @@ func TestXMRigMiner_GetStats(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestXMRigMiner_HashrateHistory(t *testing.T) {
|
||||
func TestXMRigMiner_GetStats_Bad(t *testing.T) {
|
||||
// Don't start a server, so the API call will fail
|
||||
miner := NewXMRigMiner()
|
||||
miner.Running = true // Mock running state
|
||||
miner.API.ListenHost = "127.0.0.1"
|
||||
miner.API.ListenPort = 9999 // A port that is unlikely to be in use
|
||||
|
||||
_, err := miner.GetStats()
|
||||
if err == nil {
|
||||
t.Fatalf("GetStats() did not return an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestXMRigMiner_HashrateHistory_Good(t *testing.T) {
|
||||
miner := NewXMRigMiner()
|
||||
now := time.Now()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue