github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/client/cli/run/service_test.go (about)

     1  package runtime
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/tickoalcantara12/micro/v3/service/runtime/source/git"
    12  	"github.com/onsi/gomega/types"
    13  
    14  	. "github.com/onsi/gomega"
    15  
    16  	"github.com/tickoalcantara12/micro/v3/test/fakes"
    17  )
    18  
    19  func TestFmtDuration(t *testing.T) {
    20  	tcs := []struct {
    21  		seconds  int64
    22  		expected string
    23  	}{
    24  		{seconds: 15, expected: "15s"},
    25  		{seconds: 0, expected: "0s"},
    26  		{seconds: 60, expected: "1m0s"},
    27  		{seconds: 75, expected: "1m15s"},
    28  		{seconds: 903, expected: "15m3s"},
    29  		{seconds: 4532, expected: "1h15m32s"},
    30  		{seconds: 82808, expected: "23h0m8s"},
    31  		{seconds: 86400, expected: "1d0h0m0s"},
    32  		{seconds: 1006400, expected: "11d15h33m20s"},
    33  		{seconds: 111006360, expected: "1284d19h6m0s"},
    34  	}
    35  
    36  	for i, tc := range tcs {
    37  		t.Run(fmt.Sprintf("Test %d", i), func(t *testing.T) {
    38  			res := fmtDuration(time.Duration(tc.seconds) * time.Second)
    39  			if res != tc.expected {
    40  				t.Errorf("Expected %s but got %s", tc.expected, res)
    41  			}
    42  		})
    43  	}
    44  }
    45  
    46  func TestSourceExists(t *testing.T) {
    47  	tcs := []struct {
    48  		name        string
    49  		source      string
    50  		errMatcher  types.GomegaMatcher
    51  		expectedURL string
    52  		callCount   int
    53  	}{
    54  		{name: "github-monorepo-main", source: "github.com/micro/services/helloworld", callCount: 3, expectedURL: "https://api.github.com/repos/micro/services/contents/helloworld?ref=main"},
    55  		{name: "github-monorepo-specific", source: "github.com/micro/services/hello/world@foobar", callCount: 1, expectedURL: "https://api.github.com/repos/micro/services/contents/hello/world?ref=foobar"},
    56  		{name: "github-multirepo-master", source: "github.com/micro/services", callCount: 2, expectedURL: "https://api.github.com/repos/micro/services/contents/?ref=master"},
    57  		{name: "github-multirepo-specific", source: "github.com/micro/services@foobar", callCount: 1, expectedURL: "https://api.github.com/repos/micro/services/contents/?ref=foobar"},
    58  		{name: "github-multirepo-specific-error", source: "github.com/micro/services@foobar", callCount: 1, expectedURL: "", errMatcher: HaveOccurred()},
    59  		{name: "gitlab-monorepo", source: "gitlab.com/micro-test/basic-micro-services", callCount: 1, expectedURL: "https://gitlab.com/micro-test/basic-micro-services"},
    60  		{name: "gitlab-multirepo", source: "gitlab.com/micro-test/basic-micro-services/foobar", callCount: 1, expectedURL: "https://gitlab.com/micro-test/basic-micro-services"},
    61  	}
    62  
    63  	for _, tc := range tcs {
    64  		t.Run(tc.name, func(t *testing.T) {
    65  			fakeTripper := &fakes.FakeRoundTripper{}
    66  			httpClient.Transport = fakeTripper
    67  			fakeTripper.RoundTripStub = func(request *http.Request) (*http.Response, error) {
    68  				if request.URL.String() != tc.expectedURL {
    69  					return &http.Response{
    70  						StatusCode: 404,
    71  						Body:       ioutil.NopCloser(new(bytes.Buffer)),
    72  						Header:     make(http.Header),
    73  					}, nil
    74  				}
    75  				return &http.Response{
    76  					StatusCode: 200,
    77  					Body:       ioutil.NopCloser(new(bytes.Buffer)),
    78  					Header:     make(http.Header),
    79  				}, nil
    80  			}
    81  			g := NewWithT(t)
    82  			src, err := git.ParseSource(tc.source)
    83  			g.Expect(err).To(BeNil())
    84  			err = sourceExists(src)
    85  			if tc.errMatcher != nil {
    86  				g.Expect(err).To(tc.errMatcher)
    87  			} else {
    88  				g.Expect(err).To(BeNil())
    89  			}
    90  			g.Expect(fakeTripper.RoundTripCallCount()).To(Equal(tc.callCount))
    91  		})
    92  
    93  	}
    94  }