<commit_before>require_relative '../../spec_helper'

describe '/v1/services' do

  let(:request_headers) do
    {
        'HTTP_AUTHORIZATION' => "Bearer #{valid_token.token}"
    }
  end

  let(:david) do
    user = User.create!(email: '<EMAIL>', external_id: '123456')
    grid = Grid.create!(name: 'Terminal A')
    grid.users << user

    user
  end

  let(:valid_token) do
    AccessToken.create!(user: david, scopes: ['user'])
  end

  let(:redis_service) do
    GridService.create!(
      grid: david.grids.first,
      name: 'redis',
      image_name: 'redis:2.8',
      stateful: true
    )
  end

  let(:container_stat_data) do
    {
      memory: { 'usage' => 100000 },
      cpu: { 'usage_pct' => 10 },
      network: {
          'rx_bytes' => 1397524, 'rx_packets' => 3109, 'rx_errors' => 0, 'rx_dropped' => 0, 'tx_bytes' => 1680754, 'tx_packets'=>3035, 'tx_errors'=>0, 'tx_dropped'=>0
      },
      spec: {
          'memory' => { 'limit' => 512000000},
          'cpu' => { 'limit' => 1024}
      }
    }
  end

  describe 'GET /:id' do
    it 'returns service json' do
      get "/v1/services/#{redis_service.name}", nil, request_headers
      expect(response.status).to eq(200)
      expect(json_response['id']).to eq(redis_service.name.to_s)
      expect(json_response['image']).to eq(redis_service.image_name)
    end

    it 'returns error without authorization' do
      request_headers.delete('HTTP_AUTHORIZATION')
      get "/v1/services/#{redis_service.name}", nil, request_headers
      expect(response.status).to eq(403)
    end
  end

  describe 'GET /:id/containers' do
    it 'returns service containers' do
      container = redis_service.containers.create!(name: 'redis-1')
      get "/v1/services/#{redis_service.name}/containers", nil, request_headers
      expect(response.status).to eq(200)
      expect(json_response['containers'].size).to eq(1)
      expect(json_response['containers'].first['id']).to eq(container.name.to_s)
    end
  end

  describe 'GET /:id/container_logs' do
    it 'returns service container logs' do
      container = redis_service.containers.create!(name: 'redis-1')
      container.container_logs.create!(data: 'foo', type: 'stdout', grid_service: redis_service)
      get "/v1/services/#{redis_service.name}/container_logs", nil, request_headers
      expect(response.status).to eq(200)
      expect(json_response['logs'].size).to eq(1)
      expect(json_response['logs'].first['data']).to eq('foo')
    end

    context 'when after parameter is passed' do
      it 'returns service container logs created after passed id' do
        container = redis_service.containers.create!(name: 'redis-1')
        log1 = container.container_logs.create!(data: 'foo', type: 'stdout', grid_service: redis_service)
        log2 = container.container_logs.create!(data: 'foo2', type: 'stdout', grid_service: redis_service)
        get "/v1/services/#{redis_service.name}/container_logs?from=#{log1.id}", nil, request_headers
        expect(response.status).to eq(200)
        expect(json_response['logs'].size).to eq(1)
        expect(json_response['logs'].first['data']).to eq('foo2')
      end

    end
  end

  describe 'GET /:id/stats' do
    context 'when container has stats data' do
      it 'returns service stats' do
        container = redis_service.containers.create!(name: 'redis-1')
        container.container_stats.create!(container_stat_data)
        get "/v1/services/#{redis_service.name}/stats", nil, request_headers
        expect(response.status).to eq(200)
        expect(json_response['stats'].size).to eq(1)
        expect(json_response['stats'].first.keys).to eq(%w(container_id cpu memory network))
        expect(json_response['stats'].first['container_id']).to eq(container.name.to_s)<commit_msg>update tests<commit_after>require_relative '../../spec_helper'

describe '/v1/services' do

  let(:request_headers) do
    {
        'HTTP_AUTHORIZATION' => "Bearer #{valid_token.token}"
    }
  end

  let(:david) do
    user = User.create!(email: '<EMAIL>', external_id: '123456')
    grid = Grid.create!(name: 'Terminal A')
    grid.users << user

    user
  end

  let(:valid_token) do
    AccessToken.create!(user: david, scopes: ['user'])
  end

  let(:redis_service) do
    GridService.create!(
      grid: david.grids.first,
      name: 'redis',
      image_name: 'redis:2.8',
      stateful: true
    )
  end

  let(:container_stat_data) do
    {
      memory: { 'usage' => 100000 },
      cpu: { 'usage_pct' => 10 },
      network: {
          'rx_bytes' => 1397524, 'rx_packets' => 3109, 'rx_errors' => 0, 'rx_dropped' => 0, 'tx_bytes' => 1680754, 'tx_packets'=>3035, 'tx_errors'=>0, 'tx_dropped'=>0
      },
      spec: {
          'memory' => { 'limit' => 512000000},
          'cpu' => { 'limit' => 1024}
      }
    }
  end

  describe 'GET /:id' do
    it 'returns service json' do
      get "/v1/services/#{redis_service.name}", nil, request_headers
      expect(response.status).to eq(200)
      expect(json_response['id']).to eq(redis_service.name.to_s)
      expect(json_response['image']).to eq(redis_service.image_name)
    end

    it 'returns error without authorization' do
      request_headers.delete('HTTP_AUTHORIZATION')
      get "/v1/services/#{redis_service.name}", nil, request_headers
      expect(response.status).to eq(403)
    end
  end

  describe 'GET /:id/containers' do
    it 'returns service containers' do
      container = redis_service.containers.create!(name: 'redis-1')
      get "/v1/services/#{redis_service.name}/containers", nil, request_headers
      expect(response.status).to eq(200)
      expect(json_response['containers'].size).to eq(1)
      expect(json_response['containers'].first['id']).to eq(container.name.to_s)
    end
  end

  describe 'GET /:id/container_logs' do
    it 'returns service container logs' do
      container = redis_service.containers.create!(name: 'redis-1')
      container.container_logs.create!(data: 'foo', type: 'stdout', grid_service: redis_service)
      get "/v1/services/#{redis_service.name}/container_logs", nil, request_headers
      expect(response.status).to eq(200)
      expect(json_response['logs'].size).to eq(1)
      expect(json_response['logs'].first['data']).to eq('foo')
    end

    context 'when from parameter is passed' do
      it 'returns service container logs created after passed id' do
        container = redis_service.containers.create!(name: 'redis-1')
        log1 = container.container_logs.create!(data: 'foo', type: 'stdout', grid_service: redis_service)
        log2 = container.container_logs.create!(data: 'foo2', type: 'stdout', grid_service: redis_service)
        get "/v1/services/#{redis_service.name}/container_logs?from=#{log1.id}", nil, request_headers
        expect(response.status).to eq(200)
        expect(json_response['logs'].size).to eq(1)
        expect(json_response['logs'].first['data']).to eq('foo2')
      end

    end
  end

  describe 'GET /:id/stats' do
    context 'when container has stats data' do
      it 'returns service stats' do
        container = redis_service.containers.create!(name: 'redis-1')
        container.container_stats.create!(container_stat_data)
        get "/v1/services/#{redis_service.name}/stats", nil, request_headers
        expect(response.status).to eq(200)
        expect(json_response['stats'].size).to eq(1)
        expect(json_response['stats'].first.keys).to eq(%w(container_id cpu memory network))
        expect(json_response['stats'].first['container_id']).to eq(container.name.to_s)
{'fraction_non_alphanumeric': 0.1489136195018548, 'fraction_numerical': 0.0246422893481717, 'mean_word_length': 3.3485023041474653, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4012297', 'n_tokens_mistral': 2576, 'n_tokens_neox': 2442, 'n_words': 492}