Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubevirt: add tests for Wrappers, EnhancedK8sMethods and utility classes #4696

Merged

Conversation

atiratree
Copy link
Member

also fixes few bugs which were found by the testing

@vojtechszocs @glekner @irosenzw @mareklibra @yaacov @pcbailey

@openshift-ci-robot openshift-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Mar 9, 2020
@openshift-ci-robot openshift-ci-robot added the component/kubevirt Related to kubevirt-plugin label Mar 9, 2020
@@ -0,0 +1,12 @@
import { Wrapper } from '../../wrapper';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is a simple example of a Wrapper

@@ -0,0 +1,85 @@
import { ObjectEnum } from '../../../../../constants';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is a simple example of ObjectWithTypePropertyWrapper

@atiratree atiratree force-pushed the kubevirt.wizardRefactorTests branch from 129be38 to c4c4271 Compare March 9, 2020 17:04
@atiratree
Copy link
Member Author

/retest

1 similar comment
@atiratree
Copy link
Member Author

/retest

Copy link
Contributor

@glekner glekner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great tests @suomiy

if (_.isEmpty(this.getDataVolumeTemplates())) {
delete this.data.spec.dataVolumeTemplates;
}
this.clearIfEmpty('spec.template.spec.domain.devices.disks');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we merge ensureStorages and ensureStorageConsistency into one method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, one is called before the change of data occurs and the other is called after the change of data - please see prependStorage method

await methods.k8sPatch(VirtualMachineModel, testVM, [testPatch]);
const actualState = methods.getActualState();

expect(actualState).toHaveLength(2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you destruct actualState for readability? or add a comment? I have no idea whats inside.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a better comment to the getActualState method

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and returns K8sResourceKind[]

expect(methodsUnchecked.history).toHaveLength(0);

await methods.k8sCreate(VirtualMachineModel, testVM);
expect(methods.getHistory()).toHaveLength(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you swap the expects? so first we check that there is history and then we actually test that there isn't

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the other way is better - otherwise it could happend that some object overwrites the first one we checked and we wouldn't know a difference

const results = await methods.rollback();

expect(results).toHaveLength(2);
expect(methods.getActualState()).toHaveLength(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is getActualState() empty? just to understand..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because the rollback worked and there are no living objects we created before on the cluster

blueberry?: {
color?: string;
};
strawberry?: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is 🍍 ?
😅

Copy link
Member Author

@atiratree atiratree Mar 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

~>.<

expect(emptySmoothie.hasType()).toBe(false);
expect(emptySmoothie.asResource()).toEqual({});
});
//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty comment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

});
});

it('stores inconsistent data', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this test, can you write more informative comments? thanks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a comment few lines bellow

@@ -4,7 +4,7 @@ import { apiVersionForModel, K8sKind, K8sResourceKind } from '@console/internal/
import { Wrapper } from './wrapper';
import { K8sResourceKindMethods } from '../types/types';

export class K8sResourceWrapper<
export abstract class K8sResourceWrapper<
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure there is no use case of creating an instance of K8sResourceWrapper?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better if the user creates its own definition if needed. Now it also requires model to be supplied.

@@ -2,7 +2,16 @@ import * as _ from 'lodash';

export const omitEmpty = (obj, justUndefined = false) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't there a lodash alternative so we can remove this code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have not found one

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_.compact(array)

Creates an array with all falsey values removed. The values false, null, 0, "", undefined, and NaN are falsey.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, you could use _.remove or _.without.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot be used very well. It should work on objects and arrays recursively with an option to switch between null x undefined

@atiratree atiratree force-pushed the kubevirt.wizardRefactorTests branch from c4c4271 to 314004c Compare March 10, 2020 19:29
@openshift-ci-robot openshift-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Mar 10, 2020
@atiratree
Copy link
Member Author

added few changes to the wrappers since last time

  • each K8sResource kind stores its model
  • deprecated initializeFromSimpleData in favor of simple instance init. The uses of initializeFromSimpleData still need to be removed in the followup as there are many instances of this. @vojtechszocs
  • added a simpler method for creating wrappers into EnhancedK8sMethods

@atiratree
Copy link
Member Author

also added mixins for our two diverged wrappers - just the more complicated methods for now

@atiratree atiratree closed this Mar 10, 2020
@atiratree atiratree reopened this Mar 10, 2020
@atiratree atiratree force-pushed the kubevirt.wizardRefactorTests branch from 314004c to fc77dd6 Compare March 11, 2020 13:08
methodsUnchecked.registerKind(kind);
methodsUnchecked.appendHistory(new HistoryItem(HistoryType.DELETE, data), enhancedOpts);
};
return { methods, methodsUnchecked };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to separate these two? Both should reference the same object.

Is methodsUnchecked meant to avoid further as any casts?

We could create a minimal type for methodsUnchecked (based on code that uses it) and return methods typed as e.g. BaseK8sMethods & EnhancedK8sMethods.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made a different method in the spyEnhancedK8sMethods
methods.mockK8sGet to avoid the type incompatibilities

using just methods outside of this one and using any typecasts

const expectHistory = (history, expectedHistory) => {
expect(history).toHaveLength(expectedHistory.length);

expectedHistory.forEach((expectHistoryItem, idx) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC the history objects are one-dimensional arrays.

If so, isn't expect(history).toEqual(expectedHistory) enough?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right - this is much easier to read. It was meant to print an index which failed, but I guess it is not that useful.

})
.build();

it('records history and shows actualState', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('records history and shows actualState', async () => {
it('records history', async () => {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

});
};

describe('enhancedK8sMethods.js', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
describe('enhancedK8sMethods.js', () => {
describe('enhancedK8sMethods', () => {

I'd use the logical module name to describe the test suite.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

expect.assertions(4);
let rollbackError;
try {
await methods.rollback();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use the toThrowError matcher here.

expect(methods.rollback).toThrowError('rollback');
// alternatively perform equality check against provided object
expect(methods.rollback).toThrowError(new Error('rollback'));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not work very well as the error must implement Constructable

@@ -2,7 +2,16 @@ import * as _ from 'lodash';

export const omitEmpty = (obj, justUndefined = false) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_.compact(array)

Creates an array with all falsey values removed. The values false, null, 0, "", undefined, and NaN are falsey.

@@ -2,7 +2,16 @@ import * as _ from 'lodash';

export const omitEmpty = (obj, justUndefined = false) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, you could use _.remove or _.without.

@atiratree atiratree force-pushed the kubevirt.wizardRefactorTests branch from fc77dd6 to 1e224e5 Compare March 11, 2020 18:10
@atiratree
Copy link
Member Author

fixed

@atiratree
Copy link
Member Author

/retest

- add k8sWrapperCreate method to EnhancedK8sMethods
@atiratree atiratree force-pushed the kubevirt.wizardRefactorTests branch from 1e224e5 to d4b54a4 Compare March 11, 2020 18:24
@atiratree
Copy link
Member Author

added deprecated statements to all initializeFromSimpleData functions

@vojtechszocs
Copy link
Contributor

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Mar 13, 2020
@openshift-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: suomiy, vojtechszocs

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. component/kubevirt Related to kubevirt-plugin lgtm Indicates that a PR is ready to be merged. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants