mardi 29 septembre 2020

Jest: Logic issue in testing a function

I want to test a function called getRunningContainers which basically returns all the running containers on my system. It uses 2 functions inside of it called listContainers and getContainer to work.

  async getRunningContainers(): Promise<ContainerInspectInfo[] | undefined> {
    const runningContainers: ContainerInspectInfo[] = [];
    const opts: any = {
      filters: {
        status: ['running'],
      },
    };
    try {
      const containers: ContainerInfo[] = await this.client.listContainers(
        opts
      );

      for (const c of containers) {
        const container: ContainerInterface = await this.client.getContainer(
          c.Id
        );
        const containerInspect: ContainerInspectInfo = await container.inspect();
        runningContainers.push(containerInspect);
      }

      return runningContainers;
    } catch (err) {
      console.log(`running containers error: ${err}`);
      return;
    }
  }

I am writing the following Jest test wherein the dummy_container is an object representing a container and dummy_container_response is an array of the same and dummy_info is an object representing ContainerInspectInfo. I am getting undefined returned from the getRunningContainer and I can't seem to figure out the reason.

describe('Container Client', () => {
    let containerData = dummy_container_responses;
    const MockDockerClient = {
        listContainers: (opts: any) => new Promise((resolve, reject) => {
            resolve(dummy_info);
        }),

        createContainer: (opts:any) => new Promise((resolve, reject)=> {
            resolve(dummy_container);
        }),

        getContainer: (id: any) => new Promise((resolve, reject) => {
            resolve(dummy_container)
        })
    }

    const containerClient = new Container(MockDockerClient);

    test('starting a container', async() => {
        const container = await containerClient.create(dummy_container);
        const containers = await containerClient.getRunningContainers()

        expect(containers).toEqual(dummy_container)
       
        
    })
}) 

I'm not sure if any more information is needed so please feel free to let me know if it is. Thanks!

import {
  Container as ContainerInterface,
  ContainerCreateOptions,
  ContainerInfo,
  ContainerInspectInfo,
  HostConfig,
} from 'dockerode';

export class Container {
  client: any;

  constructor(client: any) {
    this.client = client;
  }

  async create(
    opts: any
  ): Promise<ContainerInterface | undefined> {
    let container: ContainerInterface;
    const { name, Image, Cmd, HostConfig, Labels, Entrypoint, Env } = opts;
    try {
      const createOpts: ContainerCreateOptions = {
        name,
        Image,
        Cmd,
        HostConfig,
        Labels,
        Entrypoint,
        Env,
      };
      container = await this.client.createContainer(createOpts);
      return container;
    } catch (err) {
      console.log(`create container error: ${err}`);
      return;
    }
  }

  async getRunningContainers(): Promise<ContainerInspectInfo[] | undefined> {
    const runningContainers: ContainerInspectInfo[] = [];
    const opts: any = {
      filters: {
        status: ['running'],
      },
    };
    try {
      const containers: ContainerInfo[] = await this.client.listContainers(
        opts
      );

      for (const c of containers) {
        const container: ContainerInterface = await this.client.getContainer(
          c.Id
        );
        const containerInspect: ContainerInspectInfo = await container.inspect();
        runningContainers.push(containerInspect);
      }

      return runningContainers;
    } catch (err) {
      console.log(`running containers error: ${err}`);
      return;
    }
  }

  static newContainerConfig(
    oldContainer: ContainerInspectInfo,
    newImage: string
  ): ContainerCreateOptions {
    const config: ContainerCreateOptions = {
      name: oldContainer['Name'].replace('/', ''),
      Image: newImage,
      Cmd: oldContainer['Config']['Cmd'],
      HostConfig: oldContainer['HostConfig'],
      Labels: oldContainer['Config']['Labels'],
      Entrypoint: oldContainer['Config']['Entrypoint'],
      Env: oldContainer['Config']['Env'],
    };
    return config;
  }
}

Aucun commentaire:

Enregistrer un commentaire