github.com/release-engineering/exodus-rsync@v1.11.2/internal/gw/client_publish_test.go (about)

     1  package gw
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/release-engineering/exodus-rsync/internal/args"
     9  	"github.com/release-engineering/exodus-rsync/internal/log"
    10  )
    11  
    12  func TestClientPublish(t *testing.T) {
    13  	cfg := testConfig(t)
    14  
    15  	clientIface, err := Package.NewClient(context.Background(), cfg)
    16  	if clientIface == nil {
    17  		t.Errorf("failed to create client, err = %v", err)
    18  	}
    19  
    20  	ctx := context.Background()
    21  	ctx = log.NewContext(ctx, log.Package.NewLogger(args.Config{}))
    22  
    23  	gw := newFakeGw(t, clientIface.(*client))
    24  	gw.createPublishIds = append(gw.createPublishIds, "abc-123-456")
    25  
    26  	publish, err := clientIface.NewPublish(ctx)
    27  
    28  	// It should be able to create a publish
    29  	if err != nil {
    30  		t.Errorf("Failed to create publish, err = %v", err)
    31  	}
    32  
    33  	// It should have an ID
    34  	id := publish.ID()
    35  	if id != "abc-123-456" {
    36  		t.Errorf("got unexpected id %s", id)
    37  	}
    38  
    39  	// It should be able to add some items
    40  	addItems := []ItemInput{
    41  		{"/some/path", "1234", "mime/type", ""},
    42  		{"/other/path", "223344", "mime/type", ""},
    43  	}
    44  	err = publish.AddItems(ctx, addItems)
    45  	if err != nil {
    46  		t.Errorf("failed to add items to publish, err = %v", err)
    47  	}
    48  
    49  	// Those items should have made it in
    50  	gotItems := gw.publishes[publish.ID()].items
    51  	if !reflect.DeepEqual(gotItems, addItems) {
    52  		t.Errorf("publish state incorrect after adding items, have items: %v", gotItems)
    53  	}
    54  
    55  	// If task transitions to FAILED...
    56  	gw.publishes[publish.ID()].taskStates = []string{"NOT_STARTED", "IN_PROGRESS", "FAILED"}
    57  
    58  	// ...then a request to commit should return an error
    59  	err = publish.Commit(ctx, "")
    60  	if err == nil {
    61  		t.Errorf("unexpectedly failed to get an error from commit")
    62  	}
    63  	if err.Error() != "publish task task-abc-123-456 failed" {
    64  		t.Errorf("got unexpected error = %v", err)
    65  	}
    66  
    67  	// While if it transitions to COMPLETE...
    68  	gw.publishes[publish.ID()].taskStates = []string{"NOT_STARTED", "IN_PROGRESS", "COMPLETE"}
    69  
    70  	// We should be able to commit the result
    71  	err = publish.Commit(ctx, "")
    72  	if err != nil {
    73  		t.Errorf("unexpected error from commit: %v", err)
    74  	}
    75  
    76  	// And it should have used no specific commit mode
    77  	if gw.publishes[publish.ID()].lastCommit != "" {
    78  		t.Errorf("unexpected commit mode: %s", gw.publishes[publish.ID()].lastCommit)
    79  	}
    80  
    81  	// Let's do it again, this time with a non-blank commit mode...
    82  	gw.publishes[publish.ID()].taskStates = []string{"NOT_STARTED", "IN_PROGRESS", "COMPLETE"}
    83  	err = publish.Commit(ctx, "xyz")
    84  	if err != nil {
    85  		t.Errorf("unexpected error from commit: %v", err)
    86  	}
    87  
    88  	// Our commit mode should have made it into the endpoint
    89  	if gw.publishes[publish.ID()].lastCommit != "xyz" {
    90  		t.Errorf("unexpected commit mode: %s", gw.publishes[publish.ID()].lastCommit)
    91  	}
    92  }
    93  
    94  func TestClientGetPublish(t *testing.T) {
    95  	cfg := testConfig(t)
    96  
    97  	clientIface, err := Package.NewClient(context.Background(), cfg)
    98  	if clientIface == nil {
    99  		t.Errorf("failed to create client, err = %v", err)
   100  	}
   101  
   102  	ctx := context.Background()
   103  	ctx = log.NewContext(ctx, log.Package.NewLogger(args.Config{}))
   104  
   105  	gw := newFakeGw(t, clientIface.(*client))
   106  	gw.publishes["some-id"] = &fakePublish{id: "some-id"}
   107  
   108  	// It should be able to get a publish
   109  	var p Publish
   110  	p, err = clientIface.GetPublish(ctx, "some-id")
   111  	if err != nil {
   112  		t.Errorf("failed to get publish, err = %v", err)
   113  	}
   114  
   115  	// It should have an ID
   116  	id := p.ID()
   117  	if id != "some-id" {
   118  		t.Errorf("got unexpected id %s", id)
   119  	}
   120  
   121  	// It should be able to add some items
   122  	addItems := []ItemInput{
   123  		{"/some/path", "1234", "mime/type", ""},
   124  		{"/other/path", "223344", "mime/type", ""},
   125  	}
   126  	err = p.AddItems(ctx, addItems)
   127  	if err != nil {
   128  		t.Errorf("failed to add items to publish, err = %v", err)
   129  	}
   130  
   131  	// Those items should have made it in
   132  	gotItems := gw.publishes[p.ID()].items
   133  	if !reflect.DeepEqual(gotItems, addItems) {
   134  		t.Errorf("publish state incorrect after adding items, have items: %v", gotItems)
   135  	}
   136  
   137  }