consul/internal/resource/demo/controller_test.go

105 lines
3.1 KiB
Go
Raw Normal View History

2023-05-09 14:25:55 +00:00
// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
// SPDX-License-Identifier: BUSL-1.1
2023-05-09 14:25:55 +00:00
package demo
import (
"testing"
"github.com/stretchr/testify/require"
svctest "github.com/hashicorp/consul/agent/grpc-external/services/resource/testing"
"github.com/hashicorp/consul/internal/controller"
"github.com/hashicorp/consul/proto-public/pbresource"
pbdemov2 "github.com/hashicorp/consul/proto/private/pbdemo/v2"
"github.com/hashicorp/consul/sdk/testutil"
)
func TestArtistReconciler(t *testing.T) {
client := svctest.NewResourceServiceBuilder().
WithRegisterFns(RegisterTypes).
Run(t)
2023-05-09 14:25:55 +00:00
// Seed the database with an artist.
res, err := GenerateV2Artist()
require.NoError(t, err)
// Set the genre to BLUES to ensure there are 10 albums.
var artist pbdemov2.Artist
require.NoError(t, res.Data.UnmarshalTo(&artist))
artist.Genre = pbdemov2.Genre_GENRE_BLUES
require.NoError(t, res.Data.MarshalFrom(&artist))
ctx := testutil.TestContext(t)
writeRsp, err := client.Write(ctx, &pbresource.WriteRequest{Resource: res})
require.NoError(t, err)
// Call the reconciler for that artist.
var rec artistReconciler
runtime := controller.Runtime{
Client: client,
Logger: testutil.Logger(t),
}
req := controller.Request{
ID: writeRsp.Resource.Id,
}
require.NoError(t, rec.Reconcile(ctx, runtime, req))
// Check the status was updated.
readRsp, err := client.Read(ctx, &pbresource.ReadRequest{Id: writeRsp.Resource.Id})
require.NoError(t, err)
require.Contains(t, readRsp.Resource.Status, "consul.io/artist-controller")
status := readRsp.Resource.Status["consul.io/artist-controller"]
require.Equal(t, writeRsp.Resource.Generation, status.ObservedGeneration)
require.Len(t, status.Conditions, 11)
require.Equal(t, "Accepted", status.Conditions[0].Type)
require.Equal(t, "AlbumCreated", status.Conditions[1].Type)
// Check the albums were created.
listRsp, err := client.List(ctx, &pbresource.ListRequest{
Type: TypeV2Album,
Tenancy: readRsp.Resource.Id.Tenancy,
})
require.NoError(t, err)
require.Len(t, listRsp.Resources, 10)
// Delete an album.
_, err = client.Delete(ctx, &pbresource.DeleteRequest{Id: listRsp.Resources[0].Id})
require.NoError(t, err)
// Call the reconciler again.
require.NoError(t, rec.Reconcile(ctx, runtime, req))
// Check the album was recreated.
listRsp, err = client.List(ctx, &pbresource.ListRequest{
Type: TypeV2Album,
Tenancy: readRsp.Resource.Id.Tenancy,
})
require.NoError(t, err)
require.Len(t, listRsp.Resources, 10)
// Set the genre to DISCO.
readRsp, err = client.Read(ctx, &pbresource.ReadRequest{Id: writeRsp.Resource.Id})
require.NoError(t, err)
res = readRsp.Resource
require.NoError(t, res.Data.UnmarshalTo(&artist))
artist.Genre = pbdemov2.Genre_GENRE_DISCO
require.NoError(t, res.Data.MarshalFrom(&artist))
_, err = client.Write(ctx, &pbresource.WriteRequest{Resource: res})
require.NoError(t, err)
// Call the reconciler again.
require.NoError(t, rec.Reconcile(ctx, runtime, req))
// Check there are only 3 albums now.
listRsp, err = client.List(ctx, &pbresource.ListRequest{
Type: TypeV2Album,
Tenancy: readRsp.Resource.Id.Tenancy,
})
require.NoError(t, err)
require.Len(t, listRsp.Resources, 3)
}