github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/internal/commands/list_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/deislabs/cnab-go/claim"
     8  	"github.com/docker/app/internal/store"
     9  	"gotest.tools/assert"
    10  )
    11  
    12  type mockInstallationStore struct {
    13  	installations map[string]*store.Installation
    14  }
    15  
    16  func (m mockInstallationStore) List() ([]string, error) {
    17  	l := []string{}
    18  	for k := range m.installations {
    19  		l = append(l, k)
    20  	}
    21  	return l, nil
    22  }
    23  func (m mockInstallationStore) Store(installation *store.Installation) error { return nil }
    24  func (m mockInstallationStore) Read(installationName string) (*store.Installation, error) {
    25  	return m.installations[installationName], nil
    26  }
    27  func (m mockInstallationStore) Delete(installationName string) error { return nil }
    28  
    29  type stubServiceFetcher struct{}
    30  
    31  func (s stubServiceFetcher) getServices(*store.Installation) (appServices, error) {
    32  	return map[string]ServiceStatus{"service1": {DesiredTasks: 1, RunningTasks: 1}}, nil
    33  }
    34  
    35  func TestGetInstallationsSorted(t *testing.T) {
    36  	now := time.Now()
    37  	oldInstallation := &store.Installation{
    38  		Claim: claim.Claim{
    39  			Name:     "old-installation",
    40  			Modified: now.Add(-1 * time.Hour),
    41  		},
    42  	}
    43  	newInstallation := &store.Installation{
    44  		Claim: claim.Claim{
    45  			Name:     "new-installation",
    46  			Modified: now,
    47  		},
    48  	}
    49  	installationStore := mockInstallationStore{installations: map[string]*store.Installation{"old-installation": oldInstallation, "new-installation": newInstallation}}
    50  	installations, err := getInstallations(installationStore, &stubServiceFetcher{})
    51  	assert.NilError(t, err)
    52  	assert.Equal(t, len(installations), 2)
    53  	// First installation is the last modified
    54  	assert.Equal(t, installations[0].Name, "new-installation")
    55  	assert.Equal(t, installations[0].Services["service1"].DesiredTasks, 1)
    56  	assert.Equal(t, installations[1].Name, "old-installation")
    57  	assert.Equal(t, installations[1].Services["service1"].RunningTasks, 1)
    58  }
    59  
    60  func TestPrintServices(t *testing.T) {
    61  	testCases := []struct {
    62  		name         string
    63  		installation Installation
    64  		expected     string
    65  	}{
    66  		{
    67  			"Failed installation",
    68  			Installation{},
    69  			"N/A",
    70  		},
    71  		{
    72  			"Non running service",
    73  			Installation{Services: map[string]ServiceStatus{
    74  				"service1": {DesiredTasks: 1, RunningTasks: 0},
    75  			}},
    76  			"0/1",
    77  		},
    78  		{
    79  			"Mixed running services and non running",
    80  			Installation{Services: map[string]ServiceStatus{
    81  				"service1": {DesiredTasks: 1, RunningTasks: 0},
    82  				"service2": {DesiredTasks: 5, RunningTasks: 1},
    83  			}},
    84  			"1/2",
    85  		},
    86  	}
    87  	for _, testCase := range testCases {
    88  		t.Run(testCase.name, func(t *testing.T) {
    89  			output := printServices(testCase.installation)
    90  			assert.Equal(t, testCase.expected, output)
    91  		})
    92  	}
    93  }