File: test_pod_log.rb

package info (click to toggle)
ruby-kubeclient 4.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,112 kB
  • sloc: ruby: 4,225; makefile: 6
file content (159 lines) | stat: -rw-r--r-- 5,783 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# frozen_string_literal: true

require_relative 'test_helper'

# Pod log tests
class TestPodLog < Minitest::Test
  def test_get_pod_log
    stub_request(:get, %r{/namespaces/default/pods/[a-z0-9-]+/log})
      .to_return(body: open_test_file('pod_log.txt'),
                 status: 200)

    client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
    retrieved_log = client.get_pod_log('redis-master-pod', 'default')

    assert_equal(open_test_file('pod_log.txt').read, retrieved_log)

    assert_requested(:get,
                     'http://localhost:8080/api/v1/namespaces/default/pods/redis-master-pod/log',
                     times: 1)
  end

  def test_get_pod_log_container
    stub_request(:get, %r{/namespaces/default/pods/[a-z0-9-]+/log})
      .to_return(body: open_test_file('pod_log.txt'),
                 status: 200)

    client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
    retrieved_log = client.get_pod_log('redis-master-pod', 'default', container: 'ruby')

    assert_equal(open_test_file('pod_log.txt').read, retrieved_log)

    assert_requested(:get,
                     'http://localhost:8080/api/v1/namespaces/default/pods/redis-master-pod/log?container=ruby',
                     times: 1)
  end

  def test_get_pod_log_since_time
    stub_request(:get, %r{/namespaces/default/pods/[a-z0-9-]+/log})
      .to_return(body: open_test_file('pod_log.txt'),
                 status: 200)

    client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
    retrieved_log = client.get_pod_log('redis-master-pod',
                                       'default',
                                       timestamps: true,
                                       since_time: '2018-04-27T18:30:17.480321984Z')

    assert_equal(open_test_file('pod_log.txt').read, retrieved_log)

    assert_requested(:get,
                     'http://localhost:8080/api/v1/namespaces/default/pods/redis-master-pod/log?sinceTime=2018-04-27T18:30:17.480321984Z&timestamps=true',
                     times: 1)
  end

  def test_get_pod_log_tail_lines
    selected_lines = open_test_file('pod_log.txt').to_a[-2..1].join

    stub_request(:get, %r{/namespaces/default/pods/[a-z0-9-]+/log})
      .to_return(body: selected_lines,
                 status: 200)

    client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
    retrieved_log = client.get_pod_log('redis-master-pod',
                                       'default',
                                       tail_lines: 2)

    assert_equal(selected_lines, retrieved_log)

    assert_requested(:get,
                     'http://localhost:8080/api/v1/namespaces/default/pods/redis-master-pod/log?tailLines=2',
                     times: 1)
  end

  def test_get_pod_limit_bytes
    selected_bytes = open_test_file('pod_log.txt').read(10)

    stub_request(:get, %r{/namespaces/default/pods/[a-z0-9-]+/log})
      .to_return(body: selected_bytes,
                 status: 200)

    client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
    retrieved_log = client.get_pod_log('redis-master-pod',
                                       'default',
                                       limit_bytes: 10)

    assert_equal(selected_bytes, retrieved_log)

    assert_requested(:get,
                     'http://localhost:8080/api/v1/namespaces/default/pods/redis-master-pod/log?limitBytes=10',
                     times: 1)
  end

  def test_watch_pod_log
    file = open_test_file('pod_log.txt')
    expected_lines = file.read.split("\n")

    stub_request(:get, %r{/namespaces/default/pods/[a-z0-9-]+/log\?.*follow})
      .to_return(body: file, status: 200)

    client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')

    stream = client.watch_pod_log('redis-master-pod', 'default')
    stream.to_enum.with_index do |notice, index|
      assert_instance_of(String, notice)
      assert_equal(expected_lines[index], notice)
    end
  end

  def test_watch_pod_log_with_block
    file = open_test_file('pod_log.txt')
    first = file.readlines.first.chomp

    stub_request(:get, %r{/namespaces/default/pods/[a-z0-9-]+/log\?.*follow})
      .to_return(body: file, status: 200)

    client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')

    client.watch_pod_log('redis-master-pod', 'default') do |line|
      assert_equal first, line
      break
    end
  end

  def test_watch_pod_log_follow_redirect
    expected_lines = open_test_file('pod_log.txt').read.split("\n")
    redirect = 'http://localhost:1234/api/namespaces/default/pods/redis-master-pod/log'

    stub_request(:get, %r{/namespaces/default/pods/[a-z0-9-]+/log\?.*follow})
      .to_return(status: 302, headers: { location: redirect })

    stub_request(:get, redirect)
      .to_return(body: open_test_file('pod_log.txt'),
                 status: 200)

    client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
    stream = client.watch_pod_log('redis-master-pod', 'default')
    stream.to_enum.with_index do |notice, index|
      assert_instance_of(String, notice)
      assert_equal(expected_lines[index], notice)
    end
  end

  def test_watch_pod_log_max_redirect
    redirect = 'http://localhost:1234/api/namespaces/default/pods/redis-master-pod/log'

    stub_request(:get, %r{/namespaces/default/pods/[a-z0-9-]+/log\?.*follow})
      .to_return(status: 302, headers: { location: redirect })

    stub_request(:get, redirect)
      .to_return(body: open_test_file('pod_log.txt'),
                 status: 200)

    client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1', http_max_redirects: 0)
    assert_raises(Kubeclient::HttpError) do
      client.watch_pod_log('redis-master-pod', 'default').each do
      end
    end
  end
end