Skip to content

Commit

Permalink
Update all call-sites to ensure ResourceRefs use cluster. (#1889)
Browse files Browse the repository at this point in the history
* Update all call-sites to ensure ResourceRefs use cluster.

Mainly tests, other than OperatorInstance.

Signed-off-by: Michael Nelson <minelson@vmware.com>

* Update extra call-sites in AppView.

* Remove optional filter arg from ResourceRef constructor.
  • Loading branch information
absoludity committed Jul 27, 2020
1 parent 5d80444 commit 55b916a
Show file tree
Hide file tree
Showing 18 changed files with 303 additions and 169 deletions.
36 changes: 22 additions & 14 deletions dashboard/src/actions/kube.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ResourceRef, { fromCRD } from "../shared/ResourceRef";
import { IClusterServiceVersionCRD, IKubeState, IResource } from "../shared/types";

const mockStore = configureMockStore([thunk]);
const clusterName = "cluster-name";

let store: any;

Expand Down Expand Up @@ -35,12 +36,12 @@ describe("getResource", () => {
const expectedActions = [
{
type: getType(actions.kube.requestResource),
payload: "api/clusters/default/api/v1/namespaces/default/services/foo",
payload: `api/clusters/${clusterName}/api/v1/namespaces/default/services/foo`,
},
{
type: getType(actions.kube.receiveResource),
payload: {
key: "api/clusters/default/api/v1/namespaces/default/services/foo",
key: `api/clusters/${clusterName}/api/v1/namespaces/default/services/foo`,
resource: [],
},
},
Expand All @@ -54,18 +55,20 @@ describe("getResource", () => {
},
} as IResource;

const ref = new ResourceRef(r);
const ref = new ResourceRef(r, clusterName);

await store.dispatch(actions.kube.getResource(ref));
expect(store.getActions()).toEqual(expectedActions);
expect(getResourceMock).toHaveBeenCalledWith("v1", "services", "default", "foo");
expect(getResourceMock).toHaveBeenCalledWith(clusterName, "v1", "services", "default", "foo");
});

it("does not fetch a resource that is already being fetched", async () => {
store = mockStore({
kube: {
items: {
"api/clusters/default/api/v1/namespaces/default/services/foo": { isFetching: true },
[`api/clusters/${clusterName}/api/v1/namespaces/default/services/foo`]: {
isFetching: true,
},
},
},
});
Expand All @@ -79,7 +82,7 @@ describe("getResource", () => {
},
} as IResource;

const ref = new ResourceRef(r);
const ref = new ResourceRef(r, clusterName);

await store.dispatch(actions.kube.getResource(ref));
expect(store.getActions()).toEqual([]);
Expand All @@ -98,12 +101,12 @@ describe("getAndWatchResource", () => {
},
} as IResource;

const ref = new ResourceRef(r);
const ref = new ResourceRef(r, clusterName);

const expectedActions = [
{
type: getType(actions.kube.requestResource),
payload: "api/clusters/default/api/v1/namespaces/default/services/foo",
payload: `api/clusters/${clusterName}/api/v1/namespaces/default/services/foo`,
},
{
type: getType(actions.kube.openWatchResource),
Expand All @@ -117,7 +120,7 @@ describe("getAndWatchResource", () => {

store.dispatch(actions.kube.getAndWatchResource(ref));
expect(store.getActions()).toEqual(expectedActions);
expect(getResourceMock).toHaveBeenCalledWith("v1", "services", "default", "foo");
expect(getResourceMock).toHaveBeenCalledWith(clusterName, "v1", "services", "default", "foo");
});

it("dispatches a getResource and openWatchResource action for a list", () => {
Expand All @@ -135,12 +138,11 @@ describe("getAndWatchResource", () => {
},
} as IResource;

const r = fromCRD(ref, "default", {});

const r = fromCRD(ref, clusterName, "default", {});
const expectedActions = [
{
type: getType(actions.kube.requestResource),
payload: "api/clusters/default/api/v1/namespaces/default/services",
payload: `api/clusters/${clusterName}/api/v1/namespaces/default/services`,
},
{
type: getType(actions.kube.openWatchResource),
Expand All @@ -155,14 +157,20 @@ describe("getAndWatchResource", () => {
store.dispatch(actions.kube.getAndWatchResource(r));
const testActions = store.getActions();
expect(testActions).toEqual(expectedActions);
expect(getResourceMock).toHaveBeenCalledWith("v1", "services", "default", undefined);
expect(getResourceMock).toHaveBeenCalledWith(
clusterName,
"v1",
"services",
"default",
undefined,
);

const watchFunction = (testActions[1].payload as any).handler as (e: any) => void;
watchFunction({ data: `{"object": ${JSON.stringify(svc)}}` });
const newAction = {
type: getType(actions.kube.receiveResourceFromList),
payload: {
key: "api/clusters/default/api/v1/namespaces/default/services",
key: `api/clusters/${clusterName}/api/v1/namespaces/default/services`,
resource: svc,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const defaultProps = {
};

const sampleResourceRef = {
cluster: "cluster-name",
apiVersion: "v1",
kind: "Secret",
name: "foo",
Expand Down
38 changes: 20 additions & 18 deletions dashboard/src/components/AppView/AppView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import AppViewComponent from "./AppView";
import ChartInfo from "./ChartInfo";
import ResourceTable from "./ResourceTable";

const clusterName = "cluster-name";

describe("AppViewComponent", () => {
// Generates a Yaml file separated by --- containing every object passed.
const generateYamlManifest = (items: any[]): string => {
Expand All @@ -40,7 +42,7 @@ describe("AppViewComponent", () => {
error: undefined,
getAppWithUpdateInfo: jest.fn(),
namespace: "my-happy-place",
cluster: "default",
cluster: clusterName,
releaseName: "mr-sunshine",
push: jest.fn(),
};
Expand Down Expand Up @@ -105,16 +107,16 @@ describe("AppViewComponent", () => {
wrapper.setProps(validProps);

expect(wrapper.state("deployRefs")).toEqual([
new ResourceRef(resources.deployment, appRelease.namespace),
new ResourceRef(resources.deployment, clusterName, appRelease.namespace),
]);
expect(wrapper.state("serviceRefs")).toEqual([
new ResourceRef(resources.service, appRelease.namespace),
new ResourceRef(resources.service, clusterName, appRelease.namespace),
]);
expect(wrapper.state("ingressRefs")).toEqual([
new ResourceRef(resources.ingress, appRelease.namespace),
new ResourceRef(resources.ingress, clusterName, appRelease.namespace),
]);
expect(wrapper.state("secretRefs")).toEqual([
new ResourceRef(resources.secret, appRelease.namespace),
new ResourceRef(resources.secret, clusterName, appRelease.namespace),
]);
});

Expand Down Expand Up @@ -270,7 +272,7 @@ describe("AppViewComponent", () => {
},
},
};
const ingressRefs = [new ResourceRef(ingress.item as IResource, "default")];
const ingressRefs = [new ResourceRef(ingress.item as IResource, clusterName, "default")];
const service = {
isFetching: false,
item: {
Expand All @@ -282,7 +284,7 @@ describe("AppViewComponent", () => {
spec: {},
},
};
const serviceRefs = [new ResourceRef(service.item as IResource, "default")];
const serviceRefs = [new ResourceRef(service.item as IResource, clusterName, "default")];

wrapper.setState({ ingressRefs, serviceRefs });

Expand All @@ -303,7 +305,7 @@ describe("AppViewComponent", () => {
},
spec: {},
};
const deployRefs = [new ResourceRef(deployment as IResource, "default")];
const deployRefs = [new ResourceRef(deployment as IResource, clusterName, "default")];

wrapper.setState({ deployRefs });

Expand All @@ -322,7 +324,7 @@ describe("AppViewComponent", () => {
},
spec: {},
};
const ref = [new ResourceRef(r as IResource, "default")];
const ref = [new ResourceRef(r as IResource, clusterName, "default")];

wrapper.setState({ statefulSetRefs: ref });

Expand All @@ -341,7 +343,7 @@ describe("AppViewComponent", () => {
},
spec: {},
};
const ref = [new ResourceRef(r as IResource, "default")];
const ref = [new ResourceRef(r as IResource, clusterName, "default")];

wrapper.setState({ daemonSetRefs: ref });

Expand Down Expand Up @@ -383,9 +385,9 @@ describe("AppViewComponent", () => {
wrapper.setProps(validProps);

expect(wrapper.state()).toMatchObject({
deployRefs: [new ResourceRef(resources.deployment, appRelease.namespace)],
serviceRefs: [new ResourceRef(resources.service, appRelease.namespace)],
otherResources: [new ResourceRef(obj, appRelease.namespace)],
deployRefs: [new ResourceRef(resources.deployment, clusterName, appRelease.namespace)],
serviceRefs: [new ResourceRef(resources.service, clusterName, appRelease.namespace)],
otherResources: [new ResourceRef(obj, clusterName, appRelease.namespace)],
});
});

Expand All @@ -403,9 +405,9 @@ describe("AppViewComponent", () => {
wrapper.setProps(validProps);

expect(wrapper.state()).toMatchObject({
deployRefs: [new ResourceRef(resources.deployment, appRelease.namespace)],
serviceRefs: [new ResourceRef(resources.service, appRelease.namespace)],
otherResources: [new ResourceRef(obj, appRelease.namespace)],
deployRefs: [new ResourceRef(resources.deployment, clusterName, appRelease.namespace)],
serviceRefs: [new ResourceRef(resources.service, clusterName, appRelease.namespace)],
otherResources: [new ResourceRef(obj, clusterName, appRelease.namespace)],
});
});

Expand All @@ -421,10 +423,10 @@ describe("AppViewComponent", () => {
expect(applicationStatus).toExist();

expect(applicationStatus.prop("statefulsetRefs")).toEqual([
new ResourceRef(resources.statefulset, appRelease.namespace),
new ResourceRef(resources.statefulset, clusterName, appRelease.namespace),
]);
expect(applicationStatus.prop("daemonsetRefs")).toEqual([
new ResourceRef(resources.daemonset, appRelease.namespace),
new ResourceRef(resources.daemonset, clusterName, appRelease.namespace),
]);
});
});
28 changes: 21 additions & 7 deletions dashboard/src/components/AppView/AppView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,25 +230,39 @@ class AppView extends React.Component<IAppViewProps, IAppViewState> {
const resource = { isFetching: true, item };
switch (i.kind) {
case "Deployment":
result.deployRefs.push(new ResourceRef(resource.item, releaseNamespace));
result.deployRefs.push(
new ResourceRef(resource.item, this.props.cluster, releaseNamespace),
);
break;
case "StatefulSet":
result.statefulSetRefs.push(new ResourceRef(resource.item, releaseNamespace));
result.statefulSetRefs.push(
new ResourceRef(resource.item, this.props.cluster, releaseNamespace),
);
break;
case "DaemonSet":
result.daemonSetRefs.push(new ResourceRef(resource.item, releaseNamespace));
result.daemonSetRefs.push(
new ResourceRef(resource.item, this.props.cluster, releaseNamespace),
);
break;
case "Service":
result.serviceRefs.push(new ResourceRef(resource.item, releaseNamespace));
result.serviceRefs.push(
new ResourceRef(resource.item, this.props.cluster, releaseNamespace),
);
break;
case "Ingress":
result.ingressRefs.push(new ResourceRef(resource.item, releaseNamespace));
result.ingressRefs.push(
new ResourceRef(resource.item, this.props.cluster, releaseNamespace),
);
break;
case "Secret":
result.secretRefs.push(new ResourceRef(resource.item, releaseNamespace));
result.secretRefs.push(
new ResourceRef(resource.item, this.props.cluster, releaseNamespace),
);
break;
default:
result.otherResources.push(new ResourceRef(resource.item, releaseNamespace));
result.otherResources.push(
new ResourceRef(resource.item, this.props.cluster, releaseNamespace),
);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { IResource } from "../../../shared/types";
import OtherResourceItem from "./ResourceItem/OtherResourceItem";
import ResourceTable from "./ResourceTable";

const clusterName = "cluster-name";

it("skips the element if there are no resources", () => {
const wrapper = shallow(<ResourceTable resourceRefs={[]} title={""} />);
expect(wrapper.find(ResourceItemContainer)).not.toExist();
Expand All @@ -15,7 +17,11 @@ it("skips the element if there are no resources", () => {

it("renders a ResourceItem", () => {
const resourceRefs = [
new ResourceRef({ kind: "Deployment", metadata: { name: "foo" } } as IResource, "default"),
new ResourceRef(
{ kind: "Deployment", metadata: { name: "foo" } } as IResource,
clusterName,
"default",
),
];
const wrapper = shallow(<ResourceTable resourceRefs={resourceRefs} title={""} />);
expect(wrapper).toMatchSnapshot();
Expand All @@ -24,8 +30,16 @@ it("renders a ResourceItem", () => {

it("renders two resources", () => {
const deployRefs = [
new ResourceRef({ kind: "Deployment", metadata: { name: "foo" } } as IResource, "default"),
new ResourceRef({ kind: "Deployment", metadata: { name: "bar" } } as IResource, "default"),
new ResourceRef(
{ kind: "Deployment", metadata: { name: "foo" } } as IResource,
clusterName,
"default",
),
new ResourceRef(
{ kind: "Deployment", metadata: { name: "bar" } } as IResource,
clusterName,
"default",
),
];
const wrapper = shallow(<ResourceTable resourceRefs={deployRefs} title={""} />);
expect(wrapper.find(ResourceItemContainer).length).toBe(2);
Expand All @@ -45,7 +59,11 @@ it("renders two resources", () => {

it("renders OtherResourceItem", () => {
const resourceRefs = [
new ResourceRef({ kind: "ConfigMap", metadata: { name: "foo" } } as IResource, "default"),
new ResourceRef(
{ kind: "ConfigMap", metadata: { name: "foo" } } as IResource,
clusterName,
"default",
),
];
const wrapper = shallow(<ResourceTable resourceRefs={resourceRefs} title={""} />);
expect(wrapper.find(OtherResourceItem)).toExist();
Expand All @@ -54,7 +72,11 @@ it("renders OtherResourceItem", () => {

it("renders OtherResource as ItemContainer", () => {
const resourceRefs = [
new ResourceRef({ kind: "ConfigMap", metadata: { name: "foo" } } as IResource, "default"),
new ResourceRef(
{ kind: "ConfigMap", metadata: { name: "foo" } } as IResource,
clusterName,
"default",
),
];
const wrapper = shallow(
<ResourceTable resourceRefs={resourceRefs} title={""} requestOtherResources={true} />,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const defaultProps = {
};

const sampleResourceRef = {
cluster: "cluster-name",
apiVersion: "v1",
kind: "Deployment",
name: "foo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ exports[`renders a ResourceItem 1`] = `
</thead>
<tbody>
<Connect(WorkloadItem)
key="api/clusters/default/apis/undefined/namespaces/default/deployments/foo"
key="api/clusters/cluster-name/api/v1/namespaces/default/deployments/foo"
resourceRef={
ResourceRef {
"apiVersion": undefined,
"cluster": "cluster-name",
"filter": undefined,
"kind": "Deployment",
"name": "foo",
Expand Down

0 comments on commit 55b916a

Please sign in to comment.